MCPcopy Index your code
hub / github.com/cortexproject/cortex / TranslateToPromqlAPIError

Function TranslateToPromqlAPIError

pkg/querier/error_translate_queryable.go:32–79  ·  view source on GitHub ↗

TranslateToPromqlAPIError converts error to one of promql.Errors for consumption in PromQL API. PromQL API only recognizes few errors, and converts everything else to HTTP status code 422. Specifically, it supports: promql.ErrQueryCanceled, mapped to 503 promql.ErrQueryTimeout, mapped to 503 pr

(err error)

Source from the content-addressed store, hash-verified

30// - vendor/github.com/prometheus/prometheus/web/api/v1/api.go, respondError function only accepts *apiError types.
31// - translation of error to *apiError happens in vendor/github.com/prometheus/prometheus/web/api/v1/api.go, returnAPIError method.
32func TranslateToPromqlAPIError(err error) error {
33 if err == nil {
34 return err
35 }
36
37 switch errors.Cause(err).(type) {
38 case promql.ErrStorage, promql.ErrTooManySamples, promql.ErrQueryCanceled, promql.ErrQueryTimeout:
39 // Don't translate those, just in case we use them internally.
40 return err
41 case validation.LimitError:
42 // This will be returned with status code 422 by Prometheus API.
43 return err
44 case validation.AccessDeniedError:
45 // This will be returned with status code 422 by Prometheus API.
46 return err
47 default:
48 if errors.Is(err, context.Canceled) {
49 return err // 422
50 }
51
52 if search.IsResourceExhausted(err) {
53 cause := errors.Cause(err)
54 return cause // 422
55 }
56
57 s, ok := status.FromError(err)
58
59 if !ok {
60 s, ok = status.FromError(errors.Cause(err))
61 }
62
63 if ok {
64 code := s.Code()
65
66 // Treat these as HTTP status codes, even though they are supposed to be grpc codes.
67 if code >= 400 && code < 500 {
68 // Return directly, will be mapped to 422
69 return err
70 } else if code >= 500 && code < 599 {
71 // Wrap into ErrStorage for mapping to 500
72 return promql.ErrStorage{Err: err}
73 }
74 }
75
76 // All other errors will be returned as 500.
77 return promql.ErrStorage{Err: err}
78 }
79}
80
81// ErrTranslateFn is used to translate or wrap error before returning it by functions in
82// storage.SampleAndChunkQueryable interface.

Callers 1

metricsQueryFuncFunction · 0.92

Calls 2

CauseMethod · 0.80
IsMethod · 0.45

Tested by

no test coverage detected