apierror

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package apierror provides standardized API error handling. These error types can be used across all API handlers for consistent error responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAPIError

func IsAPIError(err error) bool

IsAPIError checks if an error is an API error.

Types

type Code

type Code string

Code represents an error code.

const (
	CodeBadRequest          Code = "BAD_REQUEST"
	CodeUnauthorized        Code = "UNAUTHORIZED"
	CodeForbidden           Code = "FORBIDDEN"
	CodeNotFound            Code = "NOT_FOUND"
	CodeConflict            Code = "CONFLICT"
	CodeUnprocessableEntity Code = "UNPROCESSABLE_ENTITY"
	CodeInternalError       Code = "INTERNAL_ERROR"
	CodeServiceUnavailable  Code = "SERVICE_UNAVAILABLE"
	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

func BadRequest(message string) *Error

BadRequest creates a 400 Bad Request error.

func Conflict

func Conflict(message string) *Error

Conflict creates a 409 Conflict error.

func Forbidden

func Forbidden(message string) *Error

Forbidden creates a 403 Forbidden error.

func FromError

func FromError(err error) *Error

FromError converts any error to an API error.

func InternalError

func InternalError(err error) *Error

InternalError creates a 500 Internal Server Error.

func InternalServerError

func InternalServerError(message string) *Error

InternalServerError creates a 500 Internal Server Error with a message.

func New

func New(status int, code Code, message string) *Error

New creates a new API error.

func NotFound

func NotFound(resource string) *Error

NotFound creates a 404 Not Found error.

func RateLimitExceeded

func RateLimitExceeded() *Error

RateLimitExceeded creates a 429 Too Many Requests error.

func SafeBadRequest

func SafeBadRequest(err error) *Error

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

func SafeConflict(err error) *Error

SafeConflict creates a 409 error with a safe, generic message. Use this instead of Conflict(err.Error()) to prevent information leakage.

func SafeForbidden

func SafeForbidden(err error) *Error

SafeForbidden creates a 403 error with a safe, generic message. Use this instead of Forbidden(err.Error()) to prevent information leakage.

func SafeUnauthorized

func SafeUnauthorized(err error) *Error

SafeUnauthorized creates a 401 error with a safe, generic message. Use this instead of Unauthorized(err.Error()) to prevent information leakage.

func ServiceUnavailable

func ServiceUnavailable(message string) *Error

ServiceUnavailable creates a 503 Service Unavailable error.

func TooManyRequests

func TooManyRequests(message string) *Error

TooManyRequests creates a 429 error with a custom message.

func Unauthorized

func Unauthorized(message string) *Error

Unauthorized creates a 401 Unauthorized error.

func ValidationFailed

func ValidationFailed(message string, details any) *Error

ValidationFailed creates a 422 Unprocessable Entity error.

func Wrap

func Wrap(err error, status int, code Code, message string) *Error

Wrap wraps an existing error with API error context.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Error) ToResponse

func (e *Error) ToResponse() Response

ToResponse converts the error to a response structure.

func (*Error) ToResponseWithRequestID

func (e *Error) ToResponseWithRequestID(requestID string) Response

ToResponseWithRequestID converts the error to a response with request ID.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error.

func (*Error) WithDetails

func (e *Error) WithDetails(details any) *Error

WithDetails adds details to the error.

func (*Error) WithError

func (e *Error) WithError(err error) *Error

WithError adds an internal 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

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL