Documentation
¶
Overview ¶
Package errors provides structured error types and response helpers for the API.
Index ¶
- Constants
- func GetStackTrace() string
- func WriteError(w http.ResponseWriter, err *APIError)
- func WriteErrorWithRequestID(w http.ResponseWriter, err *APIError, requestID string)
- func WriteJSON(w http.ResponseWriter, status int, data any)
- type APIError
- func New(code, message string) *APIError
- func NewConflictError(message string) *APIError
- func NewForbiddenError(message string) *APIError
- func NewInternalError(message string) *APIError
- func NewNotFoundError(message string) *APIError
- func NewUnauthorizedError(message string) *APIError
- func NewValidationError(message string) *APIError
- func NewValidationErrorWithFields(fields ValidationErrors) *APIError
- type ErrorLogEntry
- type ValidationError
- type ValidationErrors
Constants ¶
const ( CodeValidationError = "VALIDATION_ERROR" CodeNotFound = "NOT_FOUND" CodeForbidden = "FORBIDDEN" CodeInternalError = "INTERNAL_ERROR" CodeConflict = "CONFLICT" )
Error codes for structured API responses.
Variables ¶
This section is empty.
Functions ¶
func GetStackTrace ¶
func GetStackTrace() string
GetStackTrace returns the current stack trace as a string.
func WriteError ¶
func WriteError(w http.ResponseWriter, err *APIError)
WriteError writes an APIError as a JSON response.
func WriteErrorWithRequestID ¶
func WriteErrorWithRequestID(w http.ResponseWriter, err *APIError, requestID string)
WriteErrorWithRequestID writes an APIError with the request ID set.
Types ¶
type APIError ¶
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
Details map[string]any `json:"details,omitempty"`
RequestID string `json:"request_id"`
}
APIError represents a structured API error response.
func NewConflictError ¶
NewConflictError creates a conflict error.
func NewForbiddenError ¶
NewForbiddenError creates a forbidden error.
func NewInternalError ¶
NewInternalError creates an internal server error.
func NewNotFoundError ¶
NewNotFoundError creates a not found error.
func NewUnauthorizedError ¶
NewUnauthorizedError creates an unauthorized error.
func NewValidationError ¶
NewValidationError creates a validation error.
func NewValidationErrorWithFields ¶
func NewValidationErrorWithFields(fields ValidationErrors) *APIError
NewValidationErrorWithFields creates a validation error with field-level details.
func (*APIError) HTTPStatusCode ¶
HTTPStatusCode returns the appropriate HTTP status code for the error.
func (*APIError) WithDetails ¶
WithDetails returns a copy of the error with additional details.
func (*APIError) WithRequestID ¶
WithRequestID returns a copy of the error with the request ID set.
type ErrorLogEntry ¶
type ErrorLogEntry struct {
CorrelationID string `json:"correlation_id"`
ErrorCode string `json:"error_code"`
Message string `json:"message"`
StackTrace string `json:"stack_trace"`
}
ErrorLogEntry represents a structured error log entry.
func NewErrorLogEntry ¶
func NewErrorLogEntry(correlationID, errorCode, message string) *ErrorLogEntry
NewErrorLogEntry creates a new error log entry with all required fields.
func NewErrorLogEntryFromAPIError ¶
func NewErrorLogEntryFromAPIError(err *APIError, correlationID string) *ErrorLogEntry
NewErrorLogEntryFromAPIError creates an error log entry from an APIError.
func (*ErrorLogEntry) ToSlogAttrs ¶
func (e *ErrorLogEntry) ToSlogAttrs() []any
ToSlogAttrs returns the error log entry as slog attributes for structured logging.
type ValidationError ¶
ValidationError represents a field-level validation error.
type ValidationErrors ¶
type ValidationErrors []ValidationError
ValidationErrors is a collection of field-level validation errors.
func AddFieldError ¶
func AddFieldError(field, message string) ValidationErrors
AddFieldError is a helper to create a validation error for a single field.
func (*ValidationErrors) Add ¶
func (v *ValidationErrors) Add(field, message string)
Add adds a new validation error for a field.
func (ValidationErrors) HasErrors ¶
func (v ValidationErrors) HasErrors() bool
HasErrors returns true if there are any validation errors.
func (ValidationErrors) ToAPIError ¶
func (v ValidationErrors) ToAPIError() *APIError
ToAPIError converts validation errors to an APIError with field details.