-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheall.go
55 lines (43 loc) · 904 Bytes
/
cacheall.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
package espresso
import (
"fmt"
"net/http"
)
func cacheAllError(ctx Context) error {
wr := &responseWriter{
ResponseWriter: ctx.ResponseWriter(),
}
code := http.StatusInternalServerError
defer func() {
err := checkError(ctx, recover())
if wr.hasWritten || err == nil {
return
}
if httpCoder, ok := err.(HTTPError); ok {
code = httpCoder.HTTPCode()
}
wr.WriteHeader(code)
codec := CodecsModule.Value(ctx)
if codec == nil {
fmt.Fprintf(wr, "%v", err)
return
}
_ = codec.EncodeResponse(ctx, err)
}()
ctx = ctx.WithResponseWriter(wr)
ctx.Next()
return nil
}
func checkError(ctx Context, perr any) error {
if perr != nil {
return Error(http.StatusInternalServerError, fmt.Errorf("%v", perr))
}
err := ctx.Err()
if err == nil {
return nil
}
if _, ok := err.(HTTPError); ok {
return err
}
return Error(http.StatusInternalServerError, err)
}