/
error.go
86 lines (75 loc) · 1.71 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ginhelper
import (
"net/http"
"github.com/pkg/errors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
// BizError is our custom error type.
type BizError struct {
Status int `json:"-"`
Code int `json:"code"`
Message string `json:"message,omitempty"`
Err error `json:"-"`
}
// NewBizError create new BizError.
func NewBizError(status, code int, message string) *BizError {
return &BizError{
Status: status,
Code: code,
Message: message,
Err: errors.New(message),
}
}
// NewFromError can create BizError from error.
func NewFromError(status, code int, err error) *BizError {
if err == nil {
return nil
}
return &BizError{
Status: status,
Code: code,
Message: err.Error(),
Err: err,
}
}
// Error impl golang error.Error.
func (e *BizError) Error() string {
if e.Message != "" {
return e.Message
}
return http.StatusText(e.Status)
}
// Unwrap get the inner error.
func (e *BizError) Unwrap() error {
return e.Err
}
// ReplyError handle multi error
// 1. BizError, return status: BizError.Status and BizError as json body
// 2. ValidationErrors, return status: 400, and json error body
// 3. other, return status 500 and json error body.
func ReplyError(ctx *gin.Context, err error) {
// 业务错误.
var bizError *BizError
if errors.As(err, &bizError) {
ctx.JSON(bizError.Status, bizError)
return
}
// gin binding 校验错误.
var ve validator.ValidationErrors
if errors.As(err, &ve) {
// todo: 控制是否返回错误详情
err := &BizError{
Code: 400,
Message: ve.Error(),
}
ctx.JSON(400, err)
return
}
// 其他错误.
err1 := &BizError{
Code: 500,
Message: err.Error(),
}
ctx.JSON(500, err1)
}