errors

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides structured error types and response helpers for the API.

Index

Constants

View Source
const (
	CodeValidationError = "VALIDATION_ERROR"
	CodeNotFound        = "NOT_FOUND"
	CodeUnauthorized    = "UNAUTHORIZED"
	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.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data any)

WriteJSON writes a JSON response with the given status code.

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 New

func New(code, message string) *APIError

New creates a new APIError with the given code and message.

func NewConflictError

func NewConflictError(message string) *APIError

NewConflictError creates a conflict error.

func NewForbiddenError

func NewForbiddenError(message string) *APIError

NewForbiddenError creates a forbidden error.

func NewInternalError

func NewInternalError(message string) *APIError

NewInternalError creates an internal server error.

func NewNotFoundError

func NewNotFoundError(message string) *APIError

NewNotFoundError creates a not found error.

func NewUnauthorizedError

func NewUnauthorizedError(message string) *APIError

NewUnauthorizedError creates an unauthorized error.

func NewValidationError

func NewValidationError(message string) *APIError

NewValidationError creates a validation error.

func NewValidationErrorWithFields

func NewValidationErrorWithFields(fields ValidationErrors) *APIError

NewValidationErrorWithFields creates a validation error with field-level details.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) HTTPStatusCode

func (e *APIError) HTTPStatusCode() int

HTTPStatusCode returns the appropriate HTTP status code for the error.

func (*APIError) WithDetails

func (e *APIError) WithDetails(details map[string]any) *APIError

WithDetails returns a copy of the error with additional details.

func (*APIError) WithRequestID

func (e *APIError) WithRequestID(requestID string) *APIError

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

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

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.

Jump to

Keyboard shortcuts

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