-
Notifications
You must be signed in to change notification settings - Fork 1
/
recovery.go
40 lines (36 loc) · 948 Bytes
/
recovery.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
package goi
import (
"fmt"
"net/http"
"runtime"
"strings"
)
func trace(message string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:]) // skip first 3 caller
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 metaRecovery(request *Request, response http.ResponseWriter) {
if err := recover(); err != nil {
message := fmt.Sprintf("%v", err)
response.WriteHeader(http.StatusInternalServerError)
write, _ := response.Write([]byte("Internal Server Error"))
log := fmt.Sprintf("- %v - %v %v %v byte status %v %v \nerr: %v",
request.Object.Host,
request.Object.Method,
request.Object.URL.Path,
write,
request.Object.Proto,
http.StatusInternalServerError,
trace(message),
)
engine.Log.Error(log)
}
}