-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_handler.go
45 lines (43 loc) · 908 Bytes
/
web_handler.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
package bootx
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"net/http"
)
//统一异常处理
func (this *WebX) defaultErrorHandler(err error, ctx Context) {
code := 500
rsp := make(map[string]interface{})
switch cause := errors.Cause(err).(type) {
case *echo.HTTPError:
{
msg := fmt.Sprintf("%v", cause.Message)
code = cause.Code
rsp["code"] = cause.Code
rsp["message"] = msg
}
default:
{
logger.Printf("unexpect error :%v", err)
msg := "unexpect error "
if this.conf.Debug {
msg = fmt.Sprintf("%v", err)
}
code = http.StatusInternalServerError
rsp["code"] = code
rsp["message"] = msg
}
}
// Send response
if !ctx.Response().Committed {
if ctx.Request().Method == http.MethodHead {
err = ctx.NoContent(code)
} else {
err = ctx.JSONPretty(code, rsp, jsonIndent)
}
if err != nil {
logger.Println(err)
}
}
}