/
response.go
167 lines (152 loc) · 3.79 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package ginz
import (
"net/http"
"github.com/suisrc/logger.zgo"
"github.com/suisrc/res.zgo"
)
// ResSuccess 包装响应错误
// 禁止service层调用,请使用NewSuccess替换
func ResSuccess(ctx Context, v interface{}) error {
res := res.NewSuccess(ctx, v)
//ctx.JSON(http.StatusOK, res)
//ctx.Abort()
ResJSON(ctx, http.StatusOK, res)
return res
}
// ResError 包装响应错误
// 禁止service层调用,请使用NewWarpError替换
func ResError(ctx Context, em *res.ErrorModel) error {
res := res.NewWrapError(ctx, em)
//ctx.JSON(http.StatusOK, res)
//ctx.Abort()
ResJSON(ctx, em.Status, res)
return res
}
// ResJSON 响应JSON数据
// 禁止service层调用
func ResJSON(ctx Context, status int, v interface{}) {
if ctx == nil {
return
}
buf, err := res.JSONMarshal(v)
if err != nil {
panic(err)
}
if status == 0 {
status = http.StatusOK
}
ctx.Data(status, res.ResponseTypeJSON, buf)
ctx.Abort()
// ctx.JSON(status, v)
// ctx.PureJSON(status, v)
}
// FixResponseError 上级应用已经处理了返回值
func FixResponseError(c Context, err error) bool {
switch err.(type) {
case *res.Success, *res.ErrorInfo:
ResJSON(c, http.StatusOK, err)
return true
case *res.ErrorRedirect:
status := err.(*res.ErrorRedirect).Status
if status <= 0 {
status = http.StatusSeeOther
}
c.Redirect(status, err.(*res.ErrorRedirect).Location)
return true
case *res.ErrorNone:
// do nothing
return true
case *res.ErrorModel:
em := err.(*res.ErrorModel)
ResJSON(c, em.Status, res.NewWrapError(c, em))
return true
default:
// e := err.Error()
return false
}
}
// FixResponse400Error 修复返回的异常
func FixResponse400Error(c Context, err error, errfunc func()) {
if FixResponseError(c, err) {
return
}
if errfunc != nil {
errfunc()
}
ResError(c, res.Err400BadRequest)
}
// FixResponse401Error 修复返回的异常, 注意, 401异常会导致系统重定向到登陆页面
func FixResponse401Error(c Context, err error, errfunc func()) {
if FixResponseError(c, err) {
return
}
if errfunc != nil {
errfunc()
}
ResError(c, res.Err401Unauthorized)
}
// FixResponse403Error 修复返回的异常
func FixResponse403Error(c Context, err error, errfunc func()) {
if FixResponseError(c, err) {
return
}
if errfunc != nil {
errfunc()
}
ResError(c, res.Err403Forbidden)
}
// FixResponse406Error 修复返回的异常
func FixResponse406Error(c Context, err error, errfunc func()) {
if FixResponseError(c, err) {
return
}
if errfunc != nil {
errfunc()
}
ResError(c, res.Err406NotAcceptable)
}
// FixResponse500Error 修复返回的异常
func FixResponse500Error(c Context, err error, errfunc func()) {
if FixResponseError(c, err) {
return
}
if errfunc != nil {
errfunc()
}
ResError(c, res.Err500InternalServer)
}
// FixResponse500Error2Logger 修复返回的异常
func FixResponse500Error2Logger(c Context, err error) {
FixResponse500Error(c, err, func() { logger.Errorf(c, logger.ErrorWW(err)) })
}
//=============================================================
//=============================================================
//=============================================================
//// ResErrorResBody 包装响应错误
//// 禁止service层调用
//func ResErrorResBody(ctx Context, em *res.ErrorModel) error {
// res := res.NewWrapError(ctx, em)
// ResJSONResBody(ctx, em.Status, res)
// return res
//}
//
//// ResJSONResBody 响应JSON数据
//// 禁止service层调用
//func ResJSONResBody(ctx Context, status int, v interface{}) {
// if ctx == nil {
// return
// }
// buf, err := res.JSONMarshal(v)
// if err != nil {
// panic(err)
// }
// ctx.Set(res.ResBodyKey, buf)
// if status == 0 {
// status = http.StatusOK
// }
// ctx.Data(status, res.ResponseTypeJSON, buf)
// ctx.Abort()
//
// // ctx.JSON(status, v)
// // ctx.PureJSON(status, v)
//}