-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
32 lines (26 loc) · 901 Bytes
/
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
package page
import (
"log/slog"
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
type errorData struct {
App AppData
Errors validation.List
}
func Error(tmpl template.Template, logger Logger) ErrorHandler {
return func(w http.ResponseWriter, r *http.Request, err error) {
logger.Error("request error", slog.Any("req", r), slog.Any("err", err))
if err == ErrCsrfInvalid {
w.WriteHeader(http.StatusForbidden)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
if terr := tmpl(w, &errorData{App: AppDataFromContext(r.Context())}); terr != nil {
logger.Error("error rendering page", slog.Any("req", r), slog.Any("err", terr))
http.Error(w, "Encountered an error", http.StatusInternalServerError)
}
}
}