Skip to content

Commit c35a5d5

Browse files
authored
Merge pull request #2 from LemontechSA/fix/nil-pointer-on-nil-payload
fix: nil pointer on nil payload
2 parents ecaa4a4 + 26fc636 commit c35a5d5

1 file changed

Lines changed: 96 additions & 48 deletions

File tree

errs.go

Lines changed: 96 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,20 @@ func NewErrorWrapper(
9595
err error,
9696
payload map[string]string,
9797
) error {
98-
payload["code"] = fmt.Sprint(code)
99-
payload["human_message"] = message
100-
101-
return ErrorWrapper{
98+
ew := ErrorWrapper{
10299
Action: action,
103100
Message: message,
104101
Code: code,
105102
Err: err,
106103
Payload: payload,
107104
}
105+
106+
ew.AddPayloadValues(map[string]string{
107+
"code": fmt.Sprint(code),
108+
"human_message": message,
109+
})
110+
111+
return ew
108112
}
109113

110114
// Returns a wrapped error of type Bad Request - Http code 400
@@ -115,16 +119,20 @@ func NewBadRequestError(
115119
payload map[string]string,
116120
) error {
117121
code := http.StatusBadRequest
118-
payload["code"] = fmt.Sprint(code)
119-
payload["human_message"] = message
120-
121-
return ErrorWrapper{
122+
ew := ErrorWrapper{
122123
Action: action,
123124
Message: message,
124125
Code: code,
125126
Err: err,
126127
Payload: payload,
127128
}
129+
130+
ew.AddPayloadValues(map[string]string{
131+
"code": fmt.Sprint(code),
132+
"human_message": message,
133+
})
134+
135+
return ew
128136
}
129137

130138
// Returns a wrapped error of type Unauthorized - Http code 401
@@ -135,16 +143,20 @@ func NewUnauthorizedError(
135143
payload map[string]string,
136144
) error {
137145
code := http.StatusUnauthorized
138-
payload["code"] = fmt.Sprint(code)
139-
payload["human_message"] = message
140-
141-
return ErrorWrapper{
146+
ew := ErrorWrapper{
142147
Action: action,
143148
Message: message,
144149
Code: code,
145150
Err: err,
146151
Payload: payload,
147152
}
153+
154+
ew.AddPayloadValues(map[string]string{
155+
"code": fmt.Sprint(code),
156+
"human_message": message,
157+
})
158+
159+
return ew
148160
}
149161

150162
// Returns a wrapped error of type Payment Required - Http code 402
@@ -155,16 +167,20 @@ func NewPaymentRequiredError(
155167
payload map[string]string,
156168
) error {
157169
code := http.StatusPaymentRequired
158-
payload["code"] = fmt.Sprint(code)
159-
payload["human_message"] = message
160-
161-
return ErrorWrapper{
170+
ew := ErrorWrapper{
162171
Action: action,
163172
Message: message,
164173
Code: code,
165174
Err: err,
166175
Payload: payload,
167176
}
177+
178+
ew.AddPayloadValues(map[string]string{
179+
"code": fmt.Sprint(code),
180+
"human_message": message,
181+
})
182+
183+
return ew
168184
}
169185

170186
// Returns a wrapped error of type Forbidden - Http code 403
@@ -175,16 +191,20 @@ func NewForbiddenError(
175191
payload map[string]string,
176192
) error {
177193
code := http.StatusForbidden
178-
payload["code"] = fmt.Sprint(code)
179-
payload["human_message"] = message
180-
181-
return ErrorWrapper{
194+
ew := ErrorWrapper{
182195
Action: action,
183196
Message: message,
184197
Code: code,
185198
Err: err,
186199
Payload: payload,
187200
}
201+
202+
ew.AddPayloadValues(map[string]string{
203+
"code": fmt.Sprint(code),
204+
"human_message": message,
205+
})
206+
207+
return ew
188208
}
189209

190210
// Returns a wrapped error of type Not Found - Http code 404
@@ -195,16 +215,20 @@ func NewNotFoundError(
195215
payload map[string]string,
196216
) error {
197217
code := http.StatusNotFound
198-
payload["code"] = fmt.Sprint(code)
199-
payload["human_message"] = message
200-
201-
return ErrorWrapper{
218+
ew := ErrorWrapper{
202219
Action: action,
203220
Message: message,
204221
Code: code,
205222
Err: err,
206223
Payload: payload,
207224
}
225+
226+
ew.AddPayloadValues(map[string]string{
227+
"code": fmt.Sprint(code),
228+
"human_message": message,
229+
})
230+
231+
return ew
208232
}
209233

210234
// Returns a wrapped error of type Unprocessable Entity - Http code 422
@@ -215,16 +239,20 @@ func NewUnprocessableEntityError(
215239
payload map[string]string,
216240
) error {
217241
code := http.StatusUnprocessableEntity
218-
payload["code"] = fmt.Sprint(code)
219-
payload["human_message"] = message
220-
221-
return ErrorWrapper{
242+
ew := ErrorWrapper{
222243
Action: action,
223244
Message: message,
224245
Code: code,
225246
Err: err,
226247
Payload: payload,
227248
}
249+
250+
ew.AddPayloadValues(map[string]string{
251+
"code": fmt.Sprint(code),
252+
"human_message": message,
253+
})
254+
255+
return ew
228256
}
229257

230258
// Returns a wrapped error of type Internal Server Error - Http code 500
@@ -235,16 +263,20 @@ func NewInternalServerError(
235263
payload map[string]string,
236264
) error {
237265
code := http.StatusInternalServerError
238-
payload["code"] = fmt.Sprint(code)
239-
payload["human_message"] = message
240-
241-
return ErrorWrapper{
266+
ew := ErrorWrapper{
242267
Action: action,
243268
Message: message,
244269
Code: code,
245270
Err: err,
246271
Payload: payload,
247272
}
273+
274+
ew.AddPayloadValues(map[string]string{
275+
"code": fmt.Sprint(code),
276+
"human_message": message,
277+
})
278+
279+
return ew
248280
}
249281

250282
// Returns a wrapped error of type Not Implemented - Http code 501
@@ -255,16 +287,20 @@ func NewNotImplementedError(
255287
payload map[string]string,
256288
) error {
257289
code := http.StatusNotImplemented
258-
payload["code"] = fmt.Sprint(code)
259-
payload["human_message"] = message
260-
261-
return ErrorWrapper{
290+
ew := ErrorWrapper{
262291
Action: action,
263292
Message: message,
264293
Code: code,
265294
Err: err,
266295
Payload: payload,
267296
}
297+
298+
ew.AddPayloadValues(map[string]string{
299+
"code": fmt.Sprint(code),
300+
"human_message": message,
301+
})
302+
303+
return ew
268304
}
269305

270306
// Returns a wrapped error of type Bad Gateway - Http code 502
@@ -275,16 +311,20 @@ func NewBadGatewayError(
275311
payload map[string]string,
276312
) error {
277313
code := http.StatusBadGateway
278-
payload["code"] = fmt.Sprint(code)
279-
payload["human_message"] = message
280-
281-
return ErrorWrapper{
314+
ew := ErrorWrapper{
282315
Action: action,
283316
Message: message,
284317
Code: code,
285318
Err: err,
286319
Payload: payload,
287320
}
321+
322+
ew.AddPayloadValues(map[string]string{
323+
"code": fmt.Sprint(code),
324+
"human_message": message,
325+
})
326+
327+
return ew
288328
}
289329

290330
// Returns a wrapped error of type Service Unavailable - Http code 503
@@ -295,16 +335,20 @@ func NewServiceUnavailableError(
295335
payload map[string]string,
296336
) error {
297337
code := http.StatusServiceUnavailable
298-
payload["code"] = fmt.Sprint(code)
299-
payload["human_message"] = message
300-
301-
return ErrorWrapper{
338+
ew := ErrorWrapper{
302339
Action: action,
303340
Message: message,
304341
Code: code,
305342
Err: err,
306343
Payload: payload,
307344
}
345+
346+
ew.AddPayloadValues(map[string]string{
347+
"code": fmt.Sprint(code),
348+
"human_message": message,
349+
})
350+
351+
return ew
308352
}
309353

310354
// Returns a wrapped error of type Gateway Timeout - Http code 504
@@ -315,14 +359,18 @@ func NewGatewayTimeoutError(
315359
payload map[string]string,
316360
) error {
317361
code := http.StatusGatewayTimeout
318-
payload["code"] = fmt.Sprint(code)
319-
payload["human_message"] = message
320-
321-
return ErrorWrapper{
362+
ew := ErrorWrapper{
322363
Action: action,
323364
Message: message,
324365
Code: code,
325366
Err: err,
326367
Payload: payload,
327368
}
369+
370+
ew.AddPayloadValues(map[string]string{
371+
"code": fmt.Sprint(code),
372+
"human_message": message,
373+
})
374+
375+
return ew
328376
}

0 commit comments

Comments
 (0)