-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_default_error.go
43 lines (35 loc) · 1003 Bytes
/
handler_default_error.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
package auth
import (
"errors"
"github.com/gin-gonic/gin"
)
var (
ErrMissingResult = errors.New("接口未按要求返回结果")
)
func DefaultErrorResponse(c *gin.Context, err error) {
var (
resType = GetDefaultResultType(c)
result *Result
)
switch resType {
case RspTypeTmpl:
result = NewTmplResult("error.tmpl", gin.H{"error": err.Error()})
case RspTypeString:
result = NewStringResult("<!DOCTYPE html><html lang=\"zh-CN\"><head><meta charset=\"UTF-8\"><title>500错误</title></head><body><p>执行发生错误:" + err.Error() + "</p></body></html>")
default:
DefaultJsonError(c, err.Error())
return
}
result.Response(c)
}
func DefaultJsonError(c *gin.Context, errMsg string) {
var (
code = c.GetInt(CtxKeyErrorCode)
)
result := NewJSONResult(gin.H{"code": code, "message": errMsg})
result.Response(c)
}
func DefaultJsonSuccess(c *gin.Context, data interface{}) {
result := NewJSONResult(gin.H{"code": 1, "message": "", "data": data})
result.Response(c)
}