/
handler.go
60 lines (49 loc) · 2.1 KB
/
handler.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
54
55
56
57
58
59
60
package simplehttp
import (
"net/http"
)
// ErrorHandler is an error handling function for HTTP handlers
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
// DefaultErrorHandler is the default error handling function for HTTP handlers, it
// Sets the response status based on the handler's returned error using the SimpleError to HTTP mapping.
// The DefaultErrorHandler can be changed to also provide other error handling such as logging.
var DefaultErrorHandler ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
SetStatus(w, err)
}
// Handler is analogous to http.Handler but returns an error
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request) error
}
// HandlerAdapter adapts the Handler interface to the http.Handler interface
type HandlerAdapter struct {
h Handler
errHandler ErrorHandler
}
// NewHandlerAdapter returns a HandlerAdapter that can be used with the standard library http package.
func NewHandlerAdapter(h Handler, opts ...HandlerOption) *HandlerAdapter {
ha := &HandlerAdapter{h: h, errHandler: DefaultErrorHandler}
for _, opt := range opts {
opt(ha)
}
return ha
}
// ServeHTTP calls the underlying handler's ServeHTTP method and calls SetStatus on the returned error
func (h HandlerAdapter) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
err := h.h.ServeHTTP(writer, request)
h.errHandler(writer, request, err)
}
// HandlerFunc is analogous to http.HandlerFunc but returns an error
type HandlerFunc func(http.ResponseWriter, *http.Request) error
// ServerHTTP implements the Handler interface
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
return h(w, r)
}
// Adapter returns a http.HandlerFunc which calls SetStatus on the returned error
func (h HandlerFunc) Adapter(opts ...HandlerOption) http.HandlerFunc {
return NewHandlerFuncAdapter(h, opts...)
}
// NewHandlerFuncAdapter returns a http.HandlerFunc which calls SetStatus on the returned error
func NewHandlerFuncAdapter(h HandlerFunc, opts ...HandlerOption) http.HandlerFunc {
ha := NewHandlerAdapter(h, opts...)
return ha.ServeHTTP
}