package apperr import ( "net/http" "github.com/gin-gonic/gin" ) // TODO (pipo) : could we use the stack, instead of the heap? func NewTooManyRequestsError(err error, args ...string) *Error { msg, details := parseArgs("Too many requests, please try again later", args) return &Error{Code: http.StatusTooManyRequests, Message: msg, Details: details, Err: err} } func NewBadRequestError(err error, args ...string) *Error { msg, details := parseArgs("Invalid request format", args) return &Error{Code: http.StatusBadRequest, Message: msg, Details: details, Err: err} } func NewUnauthorizedError(err error, args ...string) *Error { msg, details := parseArgs("Unauthorized to perform this action", args) return &Error{Code: http.StatusUnauthorized, Message: msg, Details: details, Err: err} } func NewForbiddenError(err error, args ...string) *Error { msg, details := parseArgs("You don't have permission to access this resource", args) return &Error{Code: http.StatusForbidden, Message: msg, Details: details, Err: err} } func NewNotFoundError(err error, args ...string) *Error { msg, details := parseArgs("The requested resource was not found", args) return &Error{Code: http.StatusNotFound, Message: msg, Details: details, Err: err} } func NewConflictError(err error, args ...string) *Error { msg, details := parseArgs("Resource already exists or conflicts with another", args) return &Error{Code: http.StatusConflict, Message: msg, Details: details, Err: err} } func NewValidationError(err error, args ...string) *Error { msg, details := parseArgs("Validation failed", args) return &Error{Code: http.StatusUnprocessableEntity, Message: msg, Details: details, Err: err} } func NewInternalServerError(err error, args ...string) *Error { msg, details := parseArgs("An internal server error occurred", args) return &Error{Code: http.StatusInternalServerError, Message: msg, Details: details, Err: err} } func NewDatabaseError(err error, args ...string) *Error { msg, details := parseArgs("A database error occurred", args) return &Error{Code: 400, Message: msg, Details: details, Err: err} } // AbortWithError adds the error to Gin's context and aborts the request func AbortWithError(c *gin.Context, err error) { c.Error(err) c.Abort() }