Documentation
¶
Overview ¶
Package apierror provides standardized API error handling. These error types can be used across all API handlers for consistent error responses.
Index ¶
- func IsAPIError(err error) bool
- type Code
- type Error
- func BadRequest(message string) *Error
- func Conflict(message string) *Error
- func Forbidden(message string) *Error
- func FromError(err error) *Error
- func InternalError(err error) *Error
- func InternalServerError(message string) *Error
- func New(status int, code Code, message string) *Error
- func NotFound(resource string) *Error
- func RateLimitExceeded() *Error
- func SafeBadRequest(err error) *Error
- func SafeConflict(err error) *Error
- func SafeForbidden(err error) *Error
- func SafeUnauthorized(err error) *Error
- func ServiceUnavailable(message string) *Error
- func TooManyRequests(message string) *Error
- func Unauthorized(message string) *Error
- func ValidationFailed(message string, details any) *Error
- func Wrap(err error, status int, code Code, message string) *Error
- func (e *Error) Error() string
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) ToResponse() Response
- func (e *Error) ToResponseWithRequestID(requestID string) Response
- func (e *Error) Unwrap() error
- func (e *Error) WithDetails(details any) *Error
- func (e *Error) WithError(err error) *Error
- func (e *Error) WriteJSON(w http.ResponseWriter)
- func (e *Error) WriteJSONWithRequestID(w http.ResponseWriter, requestID string)
- type Response
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Code ¶
type Code string
Code represents an error code.
const ( CodeBadRequest Code = "BAD_REQUEST" CodeForbidden Code = "FORBIDDEN" CodeNotFound Code = "NOT_FOUND" CodeConflict Code = "CONFLICT" CodeUnprocessableEntity Code = "UNPROCESSABLE_ENTITY" CodeInternalError Code = "INTERNAL_ERROR" CodeValidationFailed Code = "VALIDATION_FAILED" CodeRateLimitExceeded Code = "RATE_LIMIT_EXCEEDED" )
Standard error codes.
type Error ¶
type Error struct {
// HTTP status code
Status int `json:"-"`
// Machine-readable error code
Code Code `json:"code"`
// Human-readable error message
Message string `json:"message"`
// Additional error details (optional)
Details any `json:"details,omitempty"`
// Internal error (not exposed to client)
Err error `json:"-"`
}
Error represents a standardized API error.
func BadRequest ¶
BadRequest creates a 400 Bad Request error.
func InternalError ¶
InternalError creates a 500 Internal Server Error.
func InternalServerError ¶
InternalServerError creates a 500 Internal Server Error with a message.
func RateLimitExceeded ¶
func RateLimitExceeded() *Error
RateLimitExceeded creates a 429 Too Many Requests error.
func SafeBadRequest ¶
SafeBadRequest creates a 400 error with a safe, generic message. The actual error is stored internally for logging but not exposed. Use this instead of BadRequest(err.Error()) to prevent information leakage.
func SafeConflict ¶
SafeConflict creates a 409 error with a safe, generic message. Use this instead of Conflict(err.Error()) to prevent information leakage.
func SafeForbidden ¶
SafeForbidden creates a 403 error with a safe, generic message. Use this instead of Forbidden(err.Error()) to prevent information leakage.
func SafeUnauthorized ¶
SafeUnauthorized creates a 401 error with a safe, generic message. Use this instead of Unauthorized(err.Error()) to prevent information leakage.
func ServiceUnavailable ¶
ServiceUnavailable creates a 503 Service Unavailable error.
func TooManyRequests ¶
TooManyRequests creates a 429 error with a custom message.
func Unauthorized ¶
Unauthorized creates a 401 Unauthorized error.
func ValidationFailed ¶
ValidationFailed creates a 422 Unprocessable Entity error.
func (*Error) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Error) ToResponse ¶
ToResponse converts the error to a response structure.
func (*Error) ToResponseWithRequestID ¶
ToResponseWithRequestID converts the error to a response with request ID.
func (*Error) WithDetails ¶
WithDetails adds details to the error.
func (*Error) WriteJSON ¶
func (e *Error) WriteJSON(w http.ResponseWriter)
WriteJSON writes the error as JSON to the response writer.
func (*Error) WriteJSONWithRequestID ¶
func (e *Error) WriteJSONWithRequestID(w http.ResponseWriter, requestID string)
WriteJSONWithRequestID writes the error as JSON with request ID.
type Response ¶
type Response struct {
Error string `json:"error"`
Code Code `json:"code"`
Message string `json:"message"`
Details any `json:"details,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
Response represents the error response structure.
type ValidationError ¶
ValidationError represents a field validation error.
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors is a collection of validation errors.
func (*ValidationErrors) Add ¶
func (v *ValidationErrors) Add(field, message string)
Add adds a validation error.
func (ValidationErrors) HasErrors ¶
func (v ValidationErrors) HasErrors() bool
HasErrors returns true if there are validation errors.
func (ValidationErrors) ToAPIError ¶
func (v ValidationErrors) ToAPIError() *Error
ToAPIError converts validation errors to an API error.