-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror_handled.go
222 lines (179 loc) · 5.63 KB
/
error_handled.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
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
package webfmwk
import (
"errors"
"fmt"
"net/http"
)
type (
// ErrorHandled interface is used to ease the error processing.
ErrorHandled interface {
// Error implelement the Error interface.
Error() string
// GetOPCode return the http status code response associated to the error.
GetOPCode() int
// SetStatusCode set the error associated http status code.
SetStatusCode(op int) ErrorHandled
// GetContent return the error http response content.
GetContent() interface{}
// Wrap/Unwrap ?
}
errorHandled struct {
content interface{}
op int
}
// Error struct is used to answer http error.
Error struct {
e error
// Message hold the error message.
//
// Example: the impossible appened
Message string `json:"message" example:"no such resource" validate:"required"`
// Status hold the error code status.
//
// Example: 500
Status int `json:"status" validate:"required"`
}
// Response is returned in case of success.
Response struct {
// Message hold the error message.
//
// Example: action successfully completed
Message string `json:"content,omitempty"`
// Status hold the error code status.
//
// Example: 200
Status int `json:"status" example:"204" validate:"required"`
}
)
// HandleError test if the error argument implement the ErrorHandled interface
// to return a matching response. Otherwise, a 500/internal error is generated
// from the error arguent.
func HandleError(ctx Context, e error) {
var eh ErrorHandled
if errors.As(e, &eh) {
_ = ctx.JSON(eh.GetOPCode(), eh.GetContent())
return
}
_ = ctx.JSONInternalError(NewErrorFromError(e))
}
// NewResponse generate a new Response struct.
func NewResponse(str string) Response {
return Response{Message: str, Status: http.StatusOK}
}
// SetStatusCode set the response status code.
func (r *Response) SetStatusCode(op int) {
r.Status = op
}
// Error implement the Error interface.
func (a Error) Error() string {
return a.Message
}
// NewError generate a Error struct.
func NewError(err string) Error {
return Error{
Message: err,
Status: http.StatusInternalServerError,
}
}
// NewCustomWrappedError generate a Error which wrap the err parameter but
// return the msg one.
func NewCustomWrappedError(err error, msg string) Error {
return Error{
Message: msg,
e: err,
Status: http.StatusInternalServerError,
}
}
// NewErrorFromError generate a Error which wrap the err parameter.
func NewErrorFromError(err error) Error {
return Error{
Message: err.Error(),
e: err,
Status: http.StatusInternalServerError,
}
}
// SetStatusCode set the error status code.
func (a *Error) SetStatusCode(op int) {
a.Status = op
}
// Error implement the Error interface.
func (e errorHandled) Error() string {
return fmt.Sprintf("[%d]: %#v", e.op, e.content)
}
// SetStatusCode implement the ErrorHandled interface.
func (e errorHandled) SetStatusCode(op int) ErrorHandled {
e.op = op
return e
}
// GetOPCode implement the ErrorHandled interface.
func (e errorHandled) GetOPCode() int {
return e.op
}
// GetContent implement the ErrorHandled interface.
func (e errorHandled) GetContent() interface{} {
return e.content
}
func factory(op int, content interface{}) errorHandled {
ret := errorHandled{
op: op,
content: content,
}
// append status code is possible
if e, ok := content.(Error); ok {
e.SetStatusCode(op)
ret.content = e
}
return ret
}
// NewErrorHandled return a struct implementing ErrorHandled with the provided params.
func NewErrorHandled(op int, content interface{}) ErrorHandled {
return factory(op, content)
}
// NewProcessing produce an ErrorHandled struct with the status code 102.
func NewProcessing(content interface{}) ErrorHandled {
return factory(http.StatusProcessing, content)
}
// NewNoContent produce an ErrorHandled struct with the status code 204.
func NewNoContent() ErrorHandled {
return factory(http.StatusNoContent, nil)
}
// NewBadRequest produce an errorHandled with the status code 400.
func NewBadRequest(content interface{}) ErrorHandled {
return factory(http.StatusBadRequest, content)
}
// NewUnauthorized produce an ErrorHandled with the status code 401.
func NewUnauthorized(content interface{}) ErrorHandled {
return factory(http.StatusUnauthorized, content)
}
// NewForbidden produce an ErrorHandled with the status code 403.
func NewForbidden(content interface{}) ErrorHandled {
return factory(http.StatusForbidden, content)
}
// NewNotFound produce an ErrorHandled with the status code 404.
func NewNotFound(content interface{}) ErrorHandled {
return factory(http.StatusNotFound, content)
}
// NewNotAcceptable produce an ErrorHandled with the status code 406.
func NewNotAcceptable(content interface{}) ErrorHandled {
return factory(http.StatusNotAcceptable, content)
}
// NewConflict produce an ErrorHandled with the status code 409.
func NewConflict(content interface{}) ErrorHandled {
return factory(http.StatusConflict, content)
}
// NewUnprocessable produce an ErrorHandled with the status code 422.
func NewUnprocessable(content interface{}) ErrorHandled {
return factory(http.StatusUnprocessableEntity, content)
}
// NewInternal produce an ErrorHandled with the status code 500.
func NewInternal(content interface{}) ErrorHandled {
return factory(http.StatusInternalServerError, content)
}
// NewNotImplemented produce an ErrorHandled with the status code 501.
func NewNotImplemented(content interface{}) ErrorHandled {
return factory(http.StatusNotImplemented, content)
}
// NewServiceUnavailable produce an ErrorHandled with the status code 503.
func NewServiceUnavailable(content interface{}) ErrorHandled {
return factory(http.StatusServiceUnavailable, content)
}