Documentation
¶
Overview ¶
Package xerrors provides a structured error handling mechanism for HTTP applications.
It extends the standard Go error interface by embedding HTTP status codes, application-specific string codes, and error chaining (cause) capabilities. This allows handlers to return rich errors that the global error handler can automatically translate into correct HTTP responses.
Basic usage:
if err != nil {
return xerrors.Wrap(err, "DB_ERROR", "failed to query user", 500)
}
if user == nil {
return xerrors.New("USER_NOT_FOUND", "user does not exist", 404)
}
Index ¶
- Constants
- Variables
- func As(err error, target interface{}) bool
- func GetCode(err error) string
- func HTTPStatusCode(err error) int
- func Is(err, target error) bool
- func Join(errs ...error) error
- func New(code, message string, httpStatus int) error
- func Wrap(err error, code, message string, httpStatus int) error
- type CustomError
Constants ¶
const ( StatusOK = 200 StatusCreated = 201 StatusAccepted = 202 StatusNoContent = 204 StatusBadRequest = 400 StatusForbidden = 403 StatusNotFound = 404 StatusMethodNotAllowed = 405 StatusNotAcceptable = 406 StatusRequestTimeout = 408 StatusConflict = 409 StatusPayloadTooLarge = 413 StatusUnsupportedMedia = 415 StatusUnprocessableEntity = 422 StatusTooManyRequests = 429 StatusInternalServerError = 500 StatusNotImplemented = 501 StatusBadGateway = 502 StatusGatewayTimeout = 504 )
Standard HTTP status codes, mapped from net/http for convenience without importing it
Variables ¶
var ( // 4xx Client Errors ErrBadRequest = New("BAD_REQUEST", "Invalid request parameters", StatusBadRequest) ErrInvalidToken = New("INVALID_TOKEN", "Token is invalid or expired", StatusUnauthorized) ErrForbidden = New("FORBIDDEN", "You do not have permission to access this resource", StatusForbidden) ErrNotFound = New("NOT_FOUND", "Resource not found", StatusNotFound) ErrMethodNotAllowed = New("METHOD_NOT_ALLOWED", "HTTP method not allowed for this resource", StatusMethodNotAllowed) ErrNotAcceptable = New("NOT_ACCEPTABLE", "Requested format is not supported", StatusNotAcceptable) ErrRequestTimeout = New("REQUEST_TIMEOUT", "Request took too long to process", StatusRequestTimeout) ErrConflict = New("CONFLICT", "Resource already exists or conflict occurred", StatusConflict) ErrPayloadTooLarge = New("PAYLOAD_TOO_LARGE", "Request payload exceeds the maximum allowed size", StatusPayloadTooLarge) ErrUnsupportedMedia = New("UNSUPPORTED_MEDIA_TYPE", "Media type is not supported", StatusUnsupportedMedia) ErrUnprocessableEntity = New("UNPROCESSABLE_ENTITY", "Data validation failed", StatusUnprocessableEntity) ErrTooManyRequests = New("TOO_MANY_REQUESTS", "Rate limit exceeded, please try again later", StatusTooManyRequests) // 5xx Server Errors ErrInternal = New("INTERNAL_ERROR", "An unexpected internal error occurred", StatusInternalServerError) ErrNotImplemented = New("NOT_IMPLEMENTED", "This feature is not yet implemented", StatusNotImplemented) ErrBadGateway = New("BAD_GATEWAY", "Invalid response from upstream server", StatusBadGateway) ErrGatewayTimeout = New("GATEWAY_TIMEOUT", "Upstream server timed out", StatusGatewayTimeout) )
Common error variables that can be reused across services.
Functions ¶
func HTTPStatusCode ¶
HTTPStatusCode extracts the HTTP status code from an error. If it's a CustomError, it returns its status. Otherwise, 500.
Types ¶
type CustomError ¶
CustomError represents a standard application error.
func (*CustomError) Error ¶
func (e *CustomError) Error() string
Error implements the standard error interface.
func (*CustomError) Unwrap ¶
func (e *CustomError) Unwrap() error
Unwrap implements the interface for errors.Is and errors.As.