-
Notifications
You must be signed in to change notification settings - Fork 2
/
recover.go
37 lines (31 loc) · 1.1 KB
/
recover.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
package page
import (
"log/slog"
"net/http"
"runtime/debug"
"strings"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
)
func Recover(tmpl template.Template, logger Logger, bundle Bundle, next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
logger.ErrorContext(r.Context(), "recover error", slog.Any("req", r), slog.Any("err", err), slog.String("stack", string(debug.Stack())))
w.WriteHeader(http.StatusInternalServerError)
appData := AppData{CookieConsentSet: true}
if strings.HasPrefix(r.URL.Path, "/cy/") {
appData.Lang = localize.Cy
} else {
appData.Lang = localize.En
}
appData.Localizer = bundle.For(appData.Lang)
if terr := tmpl(w, &errorData{App: appData}); terr != nil {
logger.ErrorContext(r.Context(), "error rendering page", slog.Any("req", r), slog.Any("err", terr))
http.Error(w, "Encountered an error", http.StatusInternalServerError)
}
}
}()
next.ServeHTTP(w, r)
}
}