Documentation
¶
Overview ¶
Package errors provides structured error handling with context propagation and HTTP status code mapping.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
Error represents a structured error with type, message, and context.
func AsStructuredError ¶
AsStructuredError converts any error into a structured Error. If err is already an *Error, returns it unchanged. Otherwise wraps it as an internal error.
func ConflictError ¶
ConflictError creates a new conflict error (HTTP 409).
func ExternalError ¶
ExternalError creates a new external service error (HTTP 502).
func InternalError ¶
InternalError creates a new internal error (HTTP 500).
func NotFoundError ¶
NotFoundError creates a new not-found error (HTTP 404).
func ValidationError ¶
ValidationError creates a new validation error (HTTP 400).
func (*Error) HTTPStatus ¶
HTTPStatus returns the appropriate HTTP status code for this error type.
func (*Error) ToResponse ¶
func (e *Error) ToResponse() ErrorResponse
ToResponse converts an Error to an ErrorResponse for JSON serialization.
func (*Error) WithContext ¶
WithContext adds context fields to the error (chainable).
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
Type ErrorType `json:"type"`
Context map[string]any `json:"context,omitempty"`
}
ErrorResponse represents the JSON structure sent to clients.
type ErrorType ¶
type ErrorType string
ErrorType represents the category of error for metrics and response formatting.
const ( // TypeValidation indicates invalid input (HTTP 400) TypeValidation ErrorType = "validation" // TypeNotFound indicates resource not found (HTTP 404) TypeNotFound ErrorType = "not_found" // TypeConflict indicates resource conflict (HTTP 409) TypeConflict ErrorType = "conflict" // TypeInternal indicates server-side error (HTTP 500) TypeInternal ErrorType = "internal" // TypeExternal indicates external service error (HTTP 502/503) TypeExternal ErrorType = "external" )