-
Notifications
You must be signed in to change notification settings - Fork 151
/
errors.go
84 lines (67 loc) · 1.35 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
74
75
76
77
78
79
80
81
82
83
84
package webhookutils
import (
"encoding/json"
"errors"
"net/http"
hatcheterrors "github.com/hatchet-dev/hatchet/pkg/errors"
"github.com/rs/zerolog"
)
type ErrorOpts struct {
Code uint
}
func HandleAPIError(
l *zerolog.Logger,
alerter hatcheterrors.Alerter,
w http.ResponseWriter,
r *http.Request,
err error,
writeErr bool,
) {
// if the error is of type detailed error, get the code from that
detailedErr := hatcheterrors.DetailedError{}
if ok := errors.As(err, &detailedErr); ok {
if detailedErr.Code == 0 || detailedErr.Code >= http.StatusInternalServerError {
handleInternalError(
l,
alerter,
w,
r,
detailedErr,
writeErr,
)
return
} else {
w.WriteHeader(int(detailedErr.Code))
writerErr := json.NewEncoder(w).Encode(detailedErr)
if writerErr != nil {
handleInternalError(
l,
alerter,
w,
r,
writerErr,
false,
)
return
}
}
return
}
return
}
func handleInternalError(l *zerolog.Logger,
alerter hatcheterrors.Alerter,
w http.ResponseWriter,
r *http.Request,
err error,
writeErr bool) {
event := l.Warn().
Str("internal_error", err.Error())
event.Send()
data := make(map[string]interface{})
data["method"] = r.Method
data["url"] = r.URL.String()
alerter.SendAlert(r.Context(), err, data)
w.WriteHeader(http.StatusInternalServerError)
return
}