-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
40 lines (30 loc) · 848 Bytes
/
handler.go
File metadata and controls
40 lines (30 loc) · 848 Bytes
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
package keratin
import (
"errors"
"net/http"
"strings"
)
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request) error
}
type HandlerFunc func(http.ResponseWriter, *http.Request) error
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
return f(w, r)
}
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
if ResponseCommitted(w) {
return
}
code := HTTPErrorStatusCode(err)
httpErr, ok := errors.AsType[*HTTPError](err)
if !ok {
httpErr = NewHTTPError(code, http.StatusText(code))
}
if strings.Contains(r.Header.Get(HeaderAccept), MIMEApplicationJSON) {
if err := JSON(w, code, httpErr); err == nil || ResponseCommitted(w) {
return
}
}
http.Error(w, httpErr.Message, code)
}