-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecovery.go
More file actions
39 lines (33 loc) · 736 Bytes
/
recovery.go
File metadata and controls
39 lines (33 loc) · 736 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
package hago
import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
)
func trace(message string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:])
var str strings.Builder
str.WriteString(message + "\nTraceback:")
for _, pc := range pcs[:n] {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
str.WriteString(fmt.Sprintf("\n\t%s:%d", file, line))
}
return str.String()
}
func Recovery() HandlerFunc {
return func(c *Context) {
defer func() {
if err := recover(); err != nil {
message := fmt.Sprintf("%s", err)
log.Printf("%s\n\n", trace(message))
c.Writer.WriteHeader(http.StatusInternalServerError)
c.Writer.Write([]byte("Internal Server Error"))
}
}()
c.Next()
}
}