forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
73 lines (63 loc) · 1.71 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ingester
import (
"fmt"
"net/http"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/weaveworks/common/httpgrpc"
)
type validationError struct {
err error // underlying error
errorType string
code int
noReport bool // if true, error will be counted but not reported to caller
labels labels.Labels
}
func makeLimitError(errorType string, err error) error {
return &validationError{
errorType: errorType,
err: err,
code: http.StatusTooManyRequests,
}
}
func makeNoReportError(errorType string) error {
return &validationError{
errorType: errorType,
noReport: true,
}
}
func makeMetricValidationError(errorType string, labels labels.Labels, err error) error {
return &validationError{
errorType: errorType,
err: err,
code: http.StatusBadRequest,
labels: labels,
}
}
func makeMetricLimitError(errorType string, labels labels.Labels, err error) error {
return &validationError{
errorType: errorType,
err: err,
code: http.StatusTooManyRequests,
labels: labels,
}
}
func (e *validationError) Error() string {
if e.err == nil {
return e.errorType
}
if e.labels == nil {
return e.err.Error()
}
return fmt.Sprintf("%s for series %s", e.err.Error(), e.labels.String())
}
// returns a HTTP gRPC error than is correctly forwarded over gRPC, with no reference to `e` retained.
func grpcForwardableError(userID string, code int, e error) error {
return httpgrpc.ErrorFromHTTPResponse(&httpgrpc.HTTPResponse{
Code: int32(code),
Body: []byte(wrapWithUser(e, userID).Error()),
})
}
// Note: does not retain a reference to `err`
func wrapWithUser(err error, userID string) error {
return fmt.Errorf("user=%s: %s", userID, err)
}