Documentation
¶
Overview ¶
Package errors provides standardized error handling for LaResto microservices. It defines common error types, HTTP status code mapping, and error wrapping utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBadRequest indicates invalid request data ErrBadRequest = &AppError{ Code: "BAD_REQUEST", Message: "Invalid request data", Status: http.StatusBadRequest, } ErrUnauthorized = &AppError{ Code: "UNAUTHORIZED", Message: "Authentication required", Status: http.StatusUnauthorized, } // ErrForbidden indicates the user lacks permission ErrForbidden = &AppError{ Code: "FORBIDDEN", Message: "Permission denied", Status: http.StatusForbidden, } // ErrNotFound indicates the requested resource was not found ErrNotFound = &AppError{ Code: "NOT_FOUND", Message: "Resource not found", Status: http.StatusNotFound, } // ErrConflict indicates a conflict with existing data ErrConflict = &AppError{ Code: "CONFLICT", Message: "Resource already exists", Status: http.StatusConflict, } // ErrValidation indicates request validation failed ErrValidation = &AppError{ Code: "VALIDATION_ERROR", Message: "Request validation failed", Status: http.StatusBadRequest, } // ErrInternal indicates an unexpected internal error ErrInternal = &AppError{ Code: "INTERNAL_ERROR", Message: "An unexpected error occurred", Status: http.StatusInternalServerError, } // ErrDatabase indicates a database operation failed ErrDatabase = &AppError{ Code: "DATABASE_ERROR", Message: "Database operation failed", Status: http.StatusInternalServerError, } // ErrCache indicates a cache operation failed ErrCache = &AppError{ Code: "CACHE_ERROR", Message: "Cache operation failed", Status: http.StatusInternalServerError, } // ErrExternal indicates an external service call failed ErrExternal = &AppError{ Code: "EXTERNAL_SERVICE_ERROR", Message: "External service unavailable", Status: http.StatusBadGateway, } )
Predefined error types for common scenarios
Functions ¶
Types ¶
type AppError ¶
type AppError struct {
// Code is a machine-readable error code (e.g., "VALIDATION_ERROR", "NOT_FOUND")
Code string `json:"code"`
// Message is a human-readable error message
Message string `json:"message"`
// Status is the HTTP status code associated with this error
Status int `json:"-"`
// Err is the underlying error (not exposed in JSON responses)
Err error `json:"-"`
// Details provides additional context (optional)
Details map[string]interface{} `json:"details,omitempty"`
}
AppError represents a standardized application error with HTTP status mapping. It contains an error code for client identification, a human-readable message, HTTP status code, and the underlying error for logging.
func Wrap ¶
Wrap wraps an existing error with additional context. If err is already an AppError, it returns it unchanged. Otherwise, it creates a new internal error wrapping the original.
func WrapExternal ¶
WrapExternal wraps an external service error with context.
func (*AppError) WithDetails ¶
WithDetails adds additional context to the error.