Skip to content

Commit

Permalink
Merge pull request #1 from LemontechSA/feat/automatic-add-code-and-hu…
Browse files Browse the repository at this point in the history
…man-message-to-payload

feat: automatic add code and human message to payload
  • Loading branch information
levelasquez authored Jul 12, 2023
2 parents b5639dc + e01c8a2 commit ecaa4a4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
70 changes: 59 additions & 11 deletions errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errs

import (
"errors"
"fmt"
"net/http"
)

Expand Down Expand Up @@ -94,6 +95,9 @@ func NewErrorWrapper(
err error,
payload map[string]string,
) error {
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Expand All @@ -110,10 +114,14 @@ func NewBadRequestError(
err error,
payload map[string]string,
) error {
code := http.StatusBadRequest
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusBadRequest,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -126,10 +134,14 @@ func NewUnauthorizedError(
err error,
payload map[string]string,
) error {
code := http.StatusUnauthorized
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusUnauthorized,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -142,10 +154,14 @@ func NewPaymentRequiredError(
err error,
payload map[string]string,
) error {
code := http.StatusPaymentRequired
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusPaymentRequired,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -158,10 +174,14 @@ func NewForbiddenError(
err error,
payload map[string]string,
) error {
code := http.StatusForbidden
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusForbidden,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -174,10 +194,14 @@ func NewNotFoundError(
err error,
payload map[string]string,
) error {
code := http.StatusNotFound
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusNotFound,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -190,10 +214,14 @@ func NewUnprocessableEntityError(
err error,
payload map[string]string,
) error {
code := http.StatusUnprocessableEntity
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusUnprocessableEntity,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -206,10 +234,14 @@ func NewInternalServerError(
err error,
payload map[string]string,
) error {
code := http.StatusInternalServerError
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusInternalServerError,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -222,10 +254,14 @@ func NewNotImplementedError(
err error,
payload map[string]string,
) error {
code := http.StatusNotImplemented
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusNotImplemented,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -238,10 +274,14 @@ func NewBadGatewayError(
err error,
payload map[string]string,
) error {
code := http.StatusBadGateway
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusBadGateway,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -254,10 +294,14 @@ func NewServiceUnavailableError(
err error,
payload map[string]string,
) error {
code := http.StatusServiceUnavailable
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusServiceUnavailable,
Code: code,
Err: err,
Payload: payload,
}
Expand All @@ -270,10 +314,14 @@ func NewGatewayTimeoutError(
err error,
payload map[string]string,
) error {
code := http.StatusGatewayTimeout
payload["code"] = fmt.Sprint(code)
payload["human_message"] = message

return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusGatewayTimeout,
Code: code,
Err: err,
Payload: payload,
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/LemontechSA/common-go-errors

go 1.19
go 1.20

0 comments on commit ecaa4a4

Please sign in to comment.