Fixed returned API status code on error (#7435)

* Fixed returned API status code on error

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Fixed linter

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Simplified code

Signed-off-by: Marco Pracucci <marco@pracucci.com>
This commit is contained in:
Marco Pracucci 2020-06-22 16:29:35 +02:00 committed by GitHub
parent 08780a9ec9
commit 153f859b74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View file

@ -469,7 +469,7 @@ func returnAPIError(err error) *apiError {
return nil
}
switch err.(type) {
switch errors.Cause(err).(type) {
case promql.ErrQueryCanceled:
return &apiError{errorCanceled, err}
case promql.ErrQueryTimeout:

View file

@ -1193,7 +1193,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, testLabelAPI
},
},
response: map[string][]metadata{
"go_threads": []metadata{
"go_threads": {
{textparse.MetricTypeGauge, "Number of OS threads created", ""},
{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
},
@ -1279,7 +1279,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, testLabelAPI
},
},
response: map[string][]metadata{
"go_threads": []metadata{
"go_threads": {
{textparse.MetricTypeGauge, "Number of OS threads created", ""},
{textparse.MetricTypeGauge, "Number of OS threads that were created.", ""},
},
@ -2719,6 +2719,42 @@ func TestTSDBStatus(t *testing.T) {
}
}
func TestReturnAPIError(t *testing.T) {
cases := []struct {
err error
expected errorType
}{
{
err: promql.ErrStorage{Err: errors.New("storage error")},
expected: errorInternal,
}, {
err: errors.Wrap(promql.ErrStorage{Err: errors.New("storage error")}, "wrapped"),
expected: errorInternal,
}, {
err: promql.ErrQueryTimeout("timeout error"),
expected: errorTimeout,
}, {
err: errors.Wrap(promql.ErrQueryTimeout("timeout error"), "wrapped"),
expected: errorTimeout,
}, {
err: promql.ErrQueryCanceled("canceled error"),
expected: errorCanceled,
}, {
err: errors.Wrap(promql.ErrQueryCanceled("canceled error"), "wrapped"),
expected: errorCanceled,
}, {
err: errors.New("exec error"),
expected: errorExec,
},
}
for _, c := range cases {
actual := returnAPIError(c.err)
testutil.NotOk(t, actual)
testutil.Equals(t, c.expected, actual.typ)
}
}
// This is a global to avoid the benchmark being optimized away.
var testResponseWriter = httptest.ResponseRecorder{}