/
response.go
75 lines (66 loc) · 1.58 KB
/
response.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
package mago
import (
"errors"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
// Response 统一响应格式
type Response interface {
// Success 成功响应
Success(data interface{})
// Failure 错误响应-默认错误信息
Failure(code ErrorCode)
// FailureMsg 错误响应-自定义错误信息
FailureMsg(code ErrorCode, msg string)
// FailureError 错误响应-根据Error
FailureError(err error)
}
type response struct {
ctx *app.RequestContext
}
func NewResponse(ctx *app.RequestContext) Response {
return &response{ctx}
}
// Success 成功响应
func (r *response) Success(data interface{}) {
if data == nil {
data = make([]int, 0)
}
r.ctx.JSON(consts.StatusOK, utils.H{
"code": ErrNone,
"msg": "成功",
"data": data,
})
}
// Failure 错误响应-默认错误信息
func (r *response) Failure(code ErrorCode) {
r.failure(code, "")
}
// FailureMsg 错误响应-自定义错误信息
func (r *response) FailureMsg(code ErrorCode, msg string) {
r.failure(code, msg)
}
// FailureError 错误响应-根据Error
func (r *response) FailureError(e error) {
var err Error
ok := errors.As(e, &err)
if ok {
r.FailureMsg(err.Code(), err.Msg())
return
}
hlog.Error(e)
r.Failure(ErrServerError)
}
func (r *response) failure(code ErrorCode, msg string) {
err := NewError(code)
if msg != "" {
err = err.WithMsg(msg)
}
r.ctx.JSON(err.HttpCode(), utils.H{
"code": int(err.Code()),
"data": make([]int, 0),
"msg": err.Msg(),
})
}