Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
levelasquez committed Mar 8, 2023
1 parent 1fe1b73 commit b5639dc
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# Dependency directories
vendor/

.DS_Store
280 changes: 280 additions & 0 deletions errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package errs

import (
"errors"
"net/http"
)

// Errors wrapper structure
type ErrorWrapper struct {
Action string `json:"action"` // Human readable action that origin the error.
Message string `json:"message"` // Human readable message for clients.
Payload map[string]string `json:"payload"` // Extra information for logs purposes.
Code int `json:"-"` // HTTP Status code. `-` is used to skip json marshaling.
Err error `json:"-"` // The original error. Same reason as above.
}

// Returns Message if Err is nil.
func (err ErrorWrapper) Error() string {
// guard against panics
if err.Err != nil {
return err.Err.Error()
}

return err.Message
}

// Implements the errors.Unwrap interface
func (err ErrorWrapper) Unwrap() error {
return err.Err // Returns inner error
}

// Returns the inner most ErrorWrapper
func (err ErrorWrapper) Dig() ErrorWrapper {
var ew ErrorWrapper

if errors.As(err.Err, &ew) {
// Recursively digs until wrapper error is not in which case it will stop
return ew.Dig()
}

return err
}

// Add a value to payload - if the key already exists the value will be overrite
func (err *ErrorWrapper) AddPayloadValue(key string, value string) {
if err.Payload == nil {
err.Payload = map[string]string{key: value}
} else {
err.Payload[key] = value
}
}

// Adds values to payload - if a key already exists the value will be overrite
func (err *ErrorWrapper) AddPayloadValues(values map[string]string) {
if err.Payload == nil {
err.Payload = values
} else {
for k, v := range values {
err.Payload[k] = v
}
}
}

// Returns the values of action and message as json
func (err ErrorWrapper) AsJSONResponse() map[string]string {
return map[string]string{
"action": err.Action,
"message": err.Message,
}
}

// Returns the inner ErrorWrapper or a generic one
func DecodeError(err error) ErrorWrapper {
var ew ErrorWrapper

if errors.As(err, &ew) {
return ew
}

return ErrorWrapper{
Action: "generic",
Message: err.Error(),
Code: http.StatusInternalServerError,
Err: err,
Payload: nil,
}
}

// Returns a generic wrapped error
func NewErrorWrapper(
action string,
message string,
code int,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: code,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Bad Request - Http code 400
func NewBadRequestError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusBadRequest,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Unauthorized - Http code 401
func NewUnauthorizedError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusUnauthorized,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Payment Required - Http code 402
func NewPaymentRequiredError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusPaymentRequired,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Forbidden - Http code 403
func NewForbiddenError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusForbidden,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Not Found - Http code 404
func NewNotFoundError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusNotFound,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Unprocessable Entity - Http code 422
func NewUnprocessableEntityError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusUnprocessableEntity,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Internal Server Error - Http code 500
func NewInternalServerError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusInternalServerError,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Not Implemented - Http code 501
func NewNotImplementedError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusNotImplemented,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Bad Gateway - Http code 502
func NewBadGatewayError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusBadGateway,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Service Unavailable - Http code 503
func NewServiceUnavailableError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusServiceUnavailable,
Err: err,
Payload: payload,
}
}

// Returns a wrapped error of type Gateway Timeout - Http code 504
func NewGatewayTimeoutError(
action string,
message string,
err error,
payload map[string]string,
) error {
return ErrorWrapper{
Action: action,
Message: message,
Code: http.StatusGatewayTimeout,
Err: err,
Payload: payload,
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/LemontechSA/common-go-errors

go 1.19

0 comments on commit b5639dc

Please sign in to comment.