-
Notifications
You must be signed in to change notification settings - Fork 1
/
recovery.go
36 lines (32 loc) · 1003 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
package http
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports/exceptions"
)
func (this *Router) recovery(request *Request, next contracts.Pipe) (result interface{}) {
defer func() {
if panicValue := recover(); panicValue != nil {
if res := this.errHandler(panicValue, request); res != nil { // 异常处理器返回的响应优先
HandleResponse(res, request)
} else {
HandleResponse(panicValue, request) // 如果异常处理器没有定义响应,则直接响应 panic 的值
}
result = nil
}
}()
return next(request)
}
func (this *Router) errHandler(err interface{}, request contracts.HttpRequest) (result interface{}) {
var httpException Exception
switch rawErr := err.(type) {
case Exception:
httpException = rawErr
default:
httpException = Exception{
Exception: exceptions.ResolveException(err),
Request: request,
}
}
// 调用容器内的异常处理器
return this.app.StaticCall(exceptionHandler, httpException)[0]
}