/
writer.go
37 lines (29 loc) · 1004 Bytes
/
writer.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 errorsx
import (
"context"
"encoding/json"
stderr "errors"
"net/http"
"github.com/pkg/errors"
"authelia.com/provider/oauth2/internal/consts"
)
// WriteJSONError is a helper function for writing errors in various scenarios. Taken from github.com/ory/herodot.
func WriteJSONError(w http.ResponseWriter, r *http.Request, err error) {
if c := StatusCodeCarrier(nil); stderr.As(err, &c) {
WriteJSONErrorCode(w, r, c.StatusCode(), err)
} else {
WriteJSONErrorCode(w, r, http.StatusInternalServerError, err)
}
}
// WriteJSONErrorCode is a helper function for writing errors in various scenarios. Taken from github.com/ory/herodot.
func WriteJSONErrorCode(w http.ResponseWriter, r *http.Request, code int, err error) {
if code == 0 {
code = http.StatusInternalServerError
}
if errors.Is(r.Context().Err(), context.Canceled) {
code = 499
}
w.Header().Set(consts.HeaderContentType, consts.ContentTypeApplicationJSON)
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(err)
}