Skip to content

Commit ecaa4a4

Browse files
authored
Merge pull request #1 from LemontechSA/feat/automatic-add-code-and-human-message-to-payload
feat: automatic add code and human message to payload
2 parents b5639dc + e01c8a2 commit ecaa4a4

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

errs.go

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errs
22

33
import (
44
"errors"
5+
"fmt"
56
"net/http"
67
)
78

@@ -94,6 +95,9 @@ func NewErrorWrapper(
9495
err error,
9596
payload map[string]string,
9697
) error {
98+
payload["code"] = fmt.Sprint(code)
99+
payload["human_message"] = message
100+
97101
return ErrorWrapper{
98102
Action: action,
99103
Message: message,
@@ -110,10 +114,14 @@ func NewBadRequestError(
110114
err error,
111115
payload map[string]string,
112116
) error {
117+
code := http.StatusBadRequest
118+
payload["code"] = fmt.Sprint(code)
119+
payload["human_message"] = message
120+
113121
return ErrorWrapper{
114122
Action: action,
115123
Message: message,
116-
Code: http.StatusBadRequest,
124+
Code: code,
117125
Err: err,
118126
Payload: payload,
119127
}
@@ -126,10 +134,14 @@ func NewUnauthorizedError(
126134
err error,
127135
payload map[string]string,
128136
) error {
137+
code := http.StatusUnauthorized
138+
payload["code"] = fmt.Sprint(code)
139+
payload["human_message"] = message
140+
129141
return ErrorWrapper{
130142
Action: action,
131143
Message: message,
132-
Code: http.StatusUnauthorized,
144+
Code: code,
133145
Err: err,
134146
Payload: payload,
135147
}
@@ -142,10 +154,14 @@ func NewPaymentRequiredError(
142154
err error,
143155
payload map[string]string,
144156
) error {
157+
code := http.StatusPaymentRequired
158+
payload["code"] = fmt.Sprint(code)
159+
payload["human_message"] = message
160+
145161
return ErrorWrapper{
146162
Action: action,
147163
Message: message,
148-
Code: http.StatusPaymentRequired,
164+
Code: code,
149165
Err: err,
150166
Payload: payload,
151167
}
@@ -158,10 +174,14 @@ func NewForbiddenError(
158174
err error,
159175
payload map[string]string,
160176
) error {
177+
code := http.StatusForbidden
178+
payload["code"] = fmt.Sprint(code)
179+
payload["human_message"] = message
180+
161181
return ErrorWrapper{
162182
Action: action,
163183
Message: message,
164-
Code: http.StatusForbidden,
184+
Code: code,
165185
Err: err,
166186
Payload: payload,
167187
}
@@ -174,10 +194,14 @@ func NewNotFoundError(
174194
err error,
175195
payload map[string]string,
176196
) error {
197+
code := http.StatusNotFound
198+
payload["code"] = fmt.Sprint(code)
199+
payload["human_message"] = message
200+
177201
return ErrorWrapper{
178202
Action: action,
179203
Message: message,
180-
Code: http.StatusNotFound,
204+
Code: code,
181205
Err: err,
182206
Payload: payload,
183207
}
@@ -190,10 +214,14 @@ func NewUnprocessableEntityError(
190214
err error,
191215
payload map[string]string,
192216
) error {
217+
code := http.StatusUnprocessableEntity
218+
payload["code"] = fmt.Sprint(code)
219+
payload["human_message"] = message
220+
193221
return ErrorWrapper{
194222
Action: action,
195223
Message: message,
196-
Code: http.StatusUnprocessableEntity,
224+
Code: code,
197225
Err: err,
198226
Payload: payload,
199227
}
@@ -206,10 +234,14 @@ func NewInternalServerError(
206234
err error,
207235
payload map[string]string,
208236
) error {
237+
code := http.StatusInternalServerError
238+
payload["code"] = fmt.Sprint(code)
239+
payload["human_message"] = message
240+
209241
return ErrorWrapper{
210242
Action: action,
211243
Message: message,
212-
Code: http.StatusInternalServerError,
244+
Code: code,
213245
Err: err,
214246
Payload: payload,
215247
}
@@ -222,10 +254,14 @@ func NewNotImplementedError(
222254
err error,
223255
payload map[string]string,
224256
) error {
257+
code := http.StatusNotImplemented
258+
payload["code"] = fmt.Sprint(code)
259+
payload["human_message"] = message
260+
225261
return ErrorWrapper{
226262
Action: action,
227263
Message: message,
228-
Code: http.StatusNotImplemented,
264+
Code: code,
229265
Err: err,
230266
Payload: payload,
231267
}
@@ -238,10 +274,14 @@ func NewBadGatewayError(
238274
err error,
239275
payload map[string]string,
240276
) error {
277+
code := http.StatusBadGateway
278+
payload["code"] = fmt.Sprint(code)
279+
payload["human_message"] = message
280+
241281
return ErrorWrapper{
242282
Action: action,
243283
Message: message,
244-
Code: http.StatusBadGateway,
284+
Code: code,
245285
Err: err,
246286
Payload: payload,
247287
}
@@ -254,10 +294,14 @@ func NewServiceUnavailableError(
254294
err error,
255295
payload map[string]string,
256296
) error {
297+
code := http.StatusServiceUnavailable
298+
payload["code"] = fmt.Sprint(code)
299+
payload["human_message"] = message
300+
257301
return ErrorWrapper{
258302
Action: action,
259303
Message: message,
260-
Code: http.StatusServiceUnavailable,
304+
Code: code,
261305
Err: err,
262306
Payload: payload,
263307
}
@@ -270,10 +314,14 @@ func NewGatewayTimeoutError(
270314
err error,
271315
payload map[string]string,
272316
) error {
317+
code := http.StatusGatewayTimeout
318+
payload["code"] = fmt.Sprint(code)
319+
payload["human_message"] = message
320+
273321
return ErrorWrapper{
274322
Action: action,
275323
Message: message,
276-
Code: http.StatusGatewayTimeout,
324+
Code: code,
277325
Err: err,
278326
Payload: payload,
279327
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/LemontechSA/common-go-errors
22

3-
go 1.19
3+
go 1.20

0 commit comments

Comments
 (0)