From 7acedbce64e36032635f230ed4f2b3e0d6487cb3 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 14 Nov 2018 15:25:54 +0530 Subject: [PATCH] web(api): Make query and range api errors match Signed-off-by: Goutham Veeramachaneni --- web/api/v1/api.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 6295564d6..f88925329 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -284,15 +284,7 @@ func (api *API) query(r *http.Request) (interface{}, *apiError, func()) { res := qry.Exec(ctx) if res.Err != nil { - switch res.Err.(type) { - case promql.ErrQueryCanceled: - return nil, &apiError{errorCanceled, res.Err}, qry.Close - case promql.ErrQueryTimeout: - return nil, &apiError{errorTimeout, res.Err}, qry.Close - case promql.ErrStorage: - return nil, &apiError{errorInternal, res.Err}, qry.Close - } - return nil, &apiError{errorExec, res.Err}, qry.Close + return nil, returnAPIError(res.Err), qry.Close } // Optional stats field in response if parameter "stats" is not empty. @@ -358,13 +350,7 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError, func()) { res := qry.Exec(ctx) if res.Err != nil { - switch res.Err.(type) { - case promql.ErrQueryCanceled: - return nil, &apiError{errorCanceled, res.Err}, qry.Close - case promql.ErrQueryTimeout: - return nil, &apiError{errorTimeout, res.Err}, qry.Close - } - return nil, &apiError{errorExec, res.Err}, qry.Close + return nil, returnAPIError(res.Err), qry.Close } // Optional stats field in response if parameter "stats" is not empty. @@ -380,6 +366,23 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError, func()) { }, nil, qry.Close } +func returnAPIError(err error) *apiError { + if err == nil { + return nil + } + + switch err.(type) { + case promql.ErrQueryCanceled: + return &apiError{errorCanceled, err} + case promql.ErrQueryTimeout: + return &apiError{errorTimeout, err} + case promql.ErrStorage: + return &apiError{errorInternal, err} + } + + return &apiError{errorExec, err} +} + func (api *API) labelValues(r *http.Request) (interface{}, *apiError, func()) { ctx := r.Context() name := route.Param(ctx, "name")