-
Notifications
You must be signed in to change notification settings - Fork 608
/
error.go
87 lines (76 loc) · 2.73 KB
/
error.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package http
import (
"context"
"encoding/json"
"errors"
"net/http"
"connectrpc.com/connect"
"github.com/grafana/dskit/httpgrpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/grafana/pyroscope/pkg/tenant"
"github.com/grafana/pyroscope/pkg/util/connectgrpc"
)
var errorWriter = connect.NewErrorWriter()
// StatusClientClosedRequest is the status code for when a client request cancellation of an http request
const StatusClientClosedRequest = 499
const (
ErrClientCanceled = "The request was cancelled by the client."
ErrDeadlineExceeded = "Request timed out, decrease the duration of the request or add more label matchers (prefer exact match over regex match) to reduce the amount of data processed."
)
// Error write a go error with the correct status code.
func Error(w http.ResponseWriter, err error) {
var connectErr *connect.Error
if ok := errors.As(err, &connectErr); ok {
writeErr := errorWriter.Write(w, &http.Request{
Header: http.Header{"Content-Type": []string{"application/json"}},
}, err)
if writeErr != nil {
http.Error(w, writeErr.Error(), http.StatusInternalServerError)
}
return
}
status, cerr := ClientHTTPStatusAndError(err)
ErrorWithStatus(w, cerr, status)
}
func ErrorWithStatus(w http.ResponseWriter, err error, status int) {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(struct {
Code connect.Code `json:"code"`
Message string `json:"message"`
}{
Code: connectgrpc.HTTPToCode(int32(status)),
Message: err.Error(),
}); err != nil {
http.Error(w, err.Error(), status)
}
}
// ClientHTTPStatusAndError returns error and http status that is "safe" to return to client without
// exposing any implementation details.
func ClientHTTPStatusAndError(err error) (int, error) {
// todo handle multi errors
// me, ok := err.(multierror.MultiError)
// if ok && me.Is(context.Canceled) {
// return StatusClientClosedRequest, errors.New(ErrClientCanceled)
// }
// if ok && me.IsDeadlineExceeded() {
// return http.StatusGatewayTimeout, errors.New(ErrDeadlineExceeded)
// }
s, isRPC := status.FromError(err)
switch {
case errors.Is(err, context.Canceled):
return StatusClientClosedRequest, errors.New(ErrClientCanceled)
case errors.Is(err, context.DeadlineExceeded) ||
(isRPC && s.Code() == codes.DeadlineExceeded):
return http.StatusGatewayTimeout, errors.New(ErrDeadlineExceeded)
case errors.Is(err, tenant.ErrNoTenantID):
return http.StatusBadRequest, err
default:
if grpcErr, ok := httpgrpc.HTTPResponseFromError(err); ok {
return int(grpcErr.Code), errors.New(string(grpcErr.Body))
}
return http.StatusInternalServerError, err
}
}