-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.go
51 lines (48 loc) · 1.22 KB
/
recover.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
package middleware
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// PanicHandler refer to https://medium.com/@masnun/panic-recovery-middleware-for-go-http-handlers-51147c941f9 and http://www.golangtraining.in/lessons/middleware/recovering-from-panic.html
func PanicHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("forcing a panic")
})
}
func Recover(log func(ctx context.Context, msg string)) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
er := recover()
if er != nil {
s := GetError(er)
log(r.Context(), s)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
jsonBody, _ := json.Marshal(map[string]string{
"error": "Internal Server Error",
})
w.Write(jsonBody)
}
}()
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}
func GetError(er interface{}) string {
if er == nil {
return "Internal Server Error"
}
switch x := er.(type) {
case string:
return er.(string)
case error:
err := x
return err.Error()
default:
return fmt.Sprintf("%v", er)
}
}