From 34f8252d3c05aacb19763251989f4c478723bde9 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Tue, 16 Feb 2021 15:07:07 +0100 Subject: [PATCH] Fixes some 500 returned by querier when storage cancellation happens. For unknown reasons, Cortex is transforming all errors to promql.ErrStorage on the storage layer. Signed-off-by: Cyril Tovena --- pkg/util/server/error.go | 9 +++++++-- pkg/util/server/error_test.go | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/util/server/error.go b/pkg/util/server/error.go index 5e2db28cc0d01..1940472816559 100644 --- a/pkg/util/server/error.go +++ b/pkg/util/server/error.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/cortexproject/cortex/pkg/chunk" + "github.com/prometheus/prometheus/promql" "github.com/weaveworks/common/httpgrpc" "github.com/weaveworks/common/user" @@ -22,10 +23,14 @@ const ( // WriteError write a go error with the correct status code. func WriteError(err error, w http.ResponseWriter) { - var queryErr chunk.QueryError + var ( + queryErr chunk.QueryError + promErr promql.ErrStorage + ) switch { - case errors.Is(err, context.Canceled): + case errors.Is(err, context.Canceled) || + (errors.As(err, &promErr) && errors.Is(promErr.Err, context.Canceled)): http.Error(w, ErrClientCanceled, StatusClientClosedRequest) case errors.Is(err, context.DeadlineExceeded): http.Error(w, ErrDeadlineExceeded, http.StatusGatewayTimeout) diff --git a/pkg/util/server/error_test.go b/pkg/util/server/error_test.go index af85c6dac5373..16133f1b76e22 100644 --- a/pkg/util/server/error_test.go +++ b/pkg/util/server/error_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/cortexproject/cortex/pkg/chunk" + "github.com/prometheus/prometheus/promql" "github.com/stretchr/testify/require" "github.com/weaveworks/common/httpgrpc" "github.com/weaveworks/common/user" @@ -28,6 +29,7 @@ func Test_writeError(t *testing.T) { }{ {"cancelled", context.Canceled, ErrClientCanceled, StatusClientClosedRequest}, {"cancelled multi", util.MultiError{context.Canceled, context.Canceled}, ErrClientCanceled, StatusClientClosedRequest}, + {"cancelled storage", promql.ErrStorage{Err: context.Canceled}, ErrClientCanceled, StatusClientClosedRequest}, {"orgid", user.ErrNoOrgID, user.ErrNoOrgID.Error(), http.StatusBadRequest}, {"deadline", context.DeadlineExceeded, ErrDeadlineExceeded, http.StatusGatewayTimeout}, {"parse error", logql.ParseError{}, "parse error : ", http.StatusBadRequest},