-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror.go
More file actions
231 lines (185 loc) · 5.88 KB
/
error.go
File metadata and controls
231 lines (185 loc) · 5.88 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package sgin
import (
"net/http"
)
// Error 是 APIError 的默认实现
type Error struct {
Code int
Message string
}
func (e *Error) Error() string {
return e.Message
}
// NewError 创建一个新的 Error
// 如果没有提供消息,将使用 http.StatusText(code) 作为默认消息
func NewError(code int, msg ...string) *Error {
if len(msg) == 0 {
return &Error{Code: code, Message: http.StatusText(code)}
}
return &Error{Code: code, Message: msg[0]}
}
// --- 常用预定义错误 (函数形式,支持自定义消息) ---
// NotModified 304
func NotModified(msg ...string) *Error {
return NewError(http.StatusNotModified, msg...)
}
// ErrBadRequest 400
func ErrBadRequest(msg ...string) *Error {
return NewError(http.StatusBadRequest, msg...)
}
// ErrUnauthorized 401
func ErrUnauthorized(msg ...string) *Error {
return NewError(http.StatusUnauthorized, msg...)
}
// ErrPaymentRequired 402
func ErrPaymentRequired(msg ...string) *Error {
return NewError(http.StatusPaymentRequired, msg...)
}
// ErrForbidden 403
func ErrForbidden(msg ...string) *Error {
return NewError(http.StatusForbidden, msg...)
}
// ErrNotFound 404
func ErrNotFound(msg ...string) *Error {
return NewError(http.StatusNotFound, msg...)
}
// ErrMethodNotAllowed 405
func ErrMethodNotAllowed(msg ...string) *Error {
return NewError(http.StatusMethodNotAllowed, msg...)
}
// ErrNotAcceptable 406
func ErrNotAcceptable(msg ...string) *Error {
return NewError(http.StatusNotAcceptable, msg...)
}
// ErrProxyAuthRequired 407
func ErrProxyAuthRequired(msg ...string) *Error {
return NewError(http.StatusProxyAuthRequired, msg...)
}
// ErrTimeout 408
func ErrTimeout(msg ...string) *Error {
return NewError(http.StatusRequestTimeout, msg...)
}
// ErrConflict 409
func ErrConflict(msg ...string) *Error {
return NewError(http.StatusConflict, msg...)
}
// ErrGone 410
func ErrGone(msg ...string) *Error {
return NewError(http.StatusGone, msg...)
}
// ErrLengthRequired 411
func ErrLengthRequired(msg ...string) *Error {
return NewError(http.StatusLengthRequired, msg...)
}
// ErrPreconditionFailed 412
func ErrPreconditionFailed(msg ...string) *Error {
return NewError(http.StatusPreconditionFailed, msg...)
}
// ErrEntityTooLarge 413
func ErrEntityTooLarge(msg ...string) *Error {
return NewError(http.StatusRequestEntityTooLarge, msg...)
}
// ErrURITooLong 414
func ErrURITooLong(msg ...string) *Error {
return NewError(http.StatusRequestURITooLong, msg...)
}
// ErrUnsupportedMediaType 415
func ErrUnsupportedMediaType(msg ...string) *Error {
return NewError(http.StatusUnsupportedMediaType, msg...)
}
// ErrRangeNotSatisfiable 416
func ErrRangeNotSatisfiable(msg ...string) *Error {
return NewError(http.StatusRequestedRangeNotSatisfiable, msg...)
}
// ErrExpectationFailed 417
func ErrExpectationFailed(msg ...string) *Error {
return NewError(http.StatusExpectationFailed, msg...)
}
// ErrTeapot 418
func ErrTeapot(msg ...string) *Error {
return NewError(http.StatusTeapot, msg...)
}
// ErrMisdirectedRequest 421
func ErrMisdirectedRequest(msg ...string) *Error {
return NewError(http.StatusMisdirectedRequest, msg...)
}
// ErrUnprocessableEntity 422
func ErrUnprocessableEntity(msg ...string) *Error {
return NewError(http.StatusUnprocessableEntity, msg...)
}
// ErrLocked 423
func ErrLocked(msg ...string) *Error {
return NewError(http.StatusLocked, msg...)
}
// ErrFailedDependency 424
func ErrFailedDependency(msg ...string) *Error {
return NewError(http.StatusFailedDependency, msg...)
}
// ErrTooEarly 425
func ErrTooEarly(msg ...string) *Error {
return NewError(http.StatusTooEarly, msg...)
}
// ErrUpgradeRequired 426
func ErrUpgradeRequired(msg ...string) *Error {
return NewError(http.StatusUpgradeRequired, msg...)
}
// ErrPreconditionRequired 428
func ErrPreconditionRequired(msg ...string) *Error {
return NewError(http.StatusPreconditionRequired, msg...)
}
// ErrTooManyRequests 429
func ErrTooManyRequests(msg ...string) *Error {
return NewError(http.StatusTooManyRequests, msg...)
}
// ErrHeaderFieldsTooLarge 431
func ErrHeaderFieldsTooLarge(msg ...string) *Error {
return NewError(http.StatusRequestHeaderFieldsTooLarge, msg...)
}
// ErrUnavailableForLegalReasons 451
func ErrUnavailableForLegalReasons(msg ...string) *Error {
return NewError(http.StatusUnavailableForLegalReasons, msg...)
}
// ErrInternalServerError 500
func ErrInternalServerError(msg ...string) *Error {
return NewError(http.StatusInternalServerError, msg...)
}
// ErrNotImplemented 501
func ErrNotImplemented(msg ...string) *Error {
return NewError(http.StatusNotImplemented, msg...)
}
// ErrBadGateway 502
func ErrBadGateway(msg ...string) *Error {
return NewError(http.StatusBadGateway, msg...)
}
// ErrServiceUnavailable 503
func ErrServiceUnavailable(msg ...string) *Error {
return NewError(http.StatusServiceUnavailable, msg...)
}
// ErrGatewayTimeout 504
func ErrGatewayTimeout(msg ...string) *Error {
return NewError(http.StatusGatewayTimeout, msg...)
}
// ErrHTTPVersionNotSupported 505
func ErrHTTPVersionNotSupported(msg ...string) *Error {
return NewError(http.StatusHTTPVersionNotSupported, msg...)
}
// ErrVariantAlsoNegotiates 506
func ErrVariantAlsoNegotiates(msg ...string) *Error {
return NewError(http.StatusVariantAlsoNegotiates, msg...)
}
// ErrInsufficientStorage 507
func ErrInsufficientStorage(msg ...string) *Error {
return NewError(http.StatusInsufficientStorage, msg...)
}
// ErrLoopDetected 508
func ErrLoopDetected(msg ...string) *Error {
return NewError(http.StatusLoopDetected, msg...)
}
// ErrNotExtended 510
func ErrNotExtended(msg ...string) *Error {
return NewError(http.StatusNotExtended, msg...)
}
// ErrNetworkAuthenticationRequired 511
func ErrNetworkAuthenticationRequired(msg ...string) *Error {
return NewError(http.StatusNetworkAuthenticationRequired, msg...)
}