-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
70 lines (58 loc) · 1.82 KB
/
handler.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
package doris
import (
"context"
"fmt"
"html/template"
"log/slog"
"net/http"
"github.com/altipla-consulting/env"
"github.com/altipla-consulting/errors"
"github.com/altipla-consulting/sentry"
"github.com/altipla-consulting/telemetry"
)
type HandlerError func(w http.ResponseWriter, r *http.Request) error
func Handler(handler HandlerError) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = sentry.WithRequest(r)
defer func() {
if rec := errors.Recover(recover()); rec != nil {
Error(w, http.StatusInternalServerError)
telemetry.ReportError(r.Context(), rec)
}
}()
if err := handler(w, r); err != nil {
if errors.Is(r.Context().Err(), context.Canceled) {
Error(w, http.StatusRequestTimeout)
return
}
slog.Error("Handler failed",
slog.String("error", err.Error()),
slog.String("details", errors.Details(err)),
slog.String("url", r.URL.String()))
telemetry.ReportError(r.Context(), err)
if errors.Is(r.Context().Err(), context.DeadlineExceeded) {
Error(w, http.StatusGatewayTimeout)
return
}
if env.IsLocal() {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, errors.Stack(err))
return
}
Error(w, http.StatusInternalServerError)
}
})
}
func Error(w http.ResponseWriter, status int) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
tmpl, err := template.New("error").Parse(errorTemplate)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
slog.Error("Cannot parse error template", slog.String("error", err.Error()))
}
if err := tmpl.Execute(w, status); err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
slog.Error("Cannot execute error template", slog.String("error", err.Error()))
}
}