/
recoverer.go
53 lines (47 loc) · 1.56 KB
/
recoverer.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
package middleware
import (
"context"
"encoding/json"
apimodel "github.com/Interhyp/metadata-service/api/v1"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/StephanHCB/go-backend-service-common/web/util/media"
"github.com/go-http-utils/headers"
"net/http"
"runtime/debug"
"time"
)
// based on the recoverer from chi, but that one wants logrus instead of zerolog and formats the stack trace with color
func PanicRecoverer(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
rvr := recover()
if rvr != nil && rvr != http.ErrAbortHandler {
ctx := r.Context()
stack := string(debug.Stack())
aulogging.Logger.Ctx(ctx).Error().Print("recovered from PANIC: " + stack)
errorHandler(ctx, w, r, "internal.error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func errorHandler(ctx context.Context, w http.ResponseWriter, _ *http.Request, msg string, status int) {
timestamp := time.Now()
response := &apimodel.ErrorDto{
Message: &msg,
Timestamp: ×tamp,
}
w.Header().Set(headers.ContentType, media.ContentTypeApplicationJson)
w.WriteHeader(status)
writeJson(ctx, w, response)
}
func writeJson(ctx context.Context, w http.ResponseWriter, v interface{}) {
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
aulogging.Logger.Ctx(ctx).Warn().WithErr(err).Printf("error while encoding json response: %v", err)
// can't change status anymore, in the middle of the response now
}
}