-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
43 lines (36 loc) · 926 Bytes
/
wrapper.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
package httputil
import (
"net/http"
)
type Wrapper struct {
SupportedErrors map[error]func(r *http.Request, err error) Response
HandleError func(r *http.Request, err any) Response
}
func (wr Wrapper) handleError(r *http.Request, rawErr any) Response {
if err, ok := rawErr.(error); ok && wr.SupportedErrors != nil {
if handler, ok := wr.SupportedErrors[err]; ok {
return handler(r, err)
}
}
if wr.HandleError != nil {
return wr.HandleError(r, rawErr)
}
return nil
}
func (wr Wrapper) Wrap(handler func(r *http.Request) (Response, error)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err any
var res Response
defer func() {
if recoverErr := recover(); recoverErr != nil {
res = wr.handleError(r, recoverErr)
} else if err != nil {
res = wr.handleError(r, err)
}
if res != nil {
_ = res.Write(w)
}
}()
res, err = handler(r)
}
}