Skip to content

Commit

Permalink
Merge pull request #2 from LemontechSA/fix/nil-pointer-on-nil-payload
Browse files Browse the repository at this point in the history
fix: nil pointer on nil payload
  • Loading branch information
levelasquez authored Jul 20, 2023
2 parents ecaa4a4 + 26fc636 commit c35a5d5
Showing 1 changed file with 96 additions and 48 deletions.
144 changes: 96 additions & 48 deletions errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,20 @@ func NewErrorWrapper(
err error,
payload map[string]string,
) error {
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Bad Request - Http code 400
Expand All @@ -115,16 +119,20 @@ func NewBadRequestError(
payload map[string]string,
) error {
code := http.StatusBadRequest
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Unauthorized - Http code 401
Expand All @@ -135,16 +143,20 @@ func NewUnauthorizedError(
payload map[string]string,
) error {
code := http.StatusUnauthorized
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Payment Required - Http code 402
Expand All @@ -155,16 +167,20 @@ func NewPaymentRequiredError(
payload map[string]string,
) error {
code := http.StatusPaymentRequired
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Forbidden - Http code 403
Expand All @@ -175,16 +191,20 @@ func NewForbiddenError(
payload map[string]string,
) error {
code := http.StatusForbidden
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Not Found - Http code 404
Expand All @@ -195,16 +215,20 @@ func NewNotFoundError(
payload map[string]string,
) error {
code := http.StatusNotFound
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Unprocessable Entity - Http code 422
Expand All @@ -215,16 +239,20 @@ func NewUnprocessableEntityError(
payload map[string]string,
) error {
code := http.StatusUnprocessableEntity
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Internal Server Error - Http code 500
Expand All @@ -235,16 +263,20 @@ func NewInternalServerError(
payload map[string]string,
) error {
code := http.StatusInternalServerError
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Not Implemented - Http code 501
Expand All @@ -255,16 +287,20 @@ func NewNotImplementedError(
payload map[string]string,
) error {
code := http.StatusNotImplemented
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Bad Gateway - Http code 502
Expand All @@ -275,16 +311,20 @@ func NewBadGatewayError(
payload map[string]string,
) error {
code := http.StatusBadGateway
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Service Unavailable - Http code 503
Expand All @@ -295,16 +335,20 @@ func NewServiceUnavailableError(
payload map[string]string,
) error {
code := http.StatusServiceUnavailable
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

// Returns a wrapped error of type Gateway Timeout - Http code 504
Expand All @@ -315,14 +359,18 @@ func NewGatewayTimeoutError(
payload map[string]string,
) error {
code := http.StatusGatewayTimeout
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
ew := ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}

ew.AddPayloadValues(map[string]string{
"code": fmt.Sprint(code),
"human_message": message,
})

return ew
}

0 comments on commit c35a5d5

Please sign in to comment.