errors

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeNotFound            = "NOT_FOUND"
	CodeBadRequest          = "BAD_REQUEST"
	CodeInternalError       = "INTERNAL_ERROR"
	CodeUnauthorized        = "UNAUTHORIZED"
	CodeForbidden           = "FORBIDDEN"
	CodeConflict            = "CONFLICT"
	CodeUnprocessable       = "UNPROCESSABLE"
	CodeValidationFailed    = "VALIDATION_FAILED"
	CodeServiceUnavailable  = "SERVICE_UNAVAILABLE"
	CodeTimeout             = "TIMEOUT"
	CodeRateLimited         = "RATE_LIMITED"
	CodePayloadTooLarge     = "PAYLOAD_TOO_LARGE"
	CodeMethodNotAllowed    = "METHOD_NOT_ALLOWED"
	CodeNotAcceptable       = "NOT_ACCEPTABLE"
	CodeGone                = "GONE"
	CodePreconditionFailed  = "PRECONDITION_FAILED"
	CodeTooManyRequests     = "TOO_MANY_REQUESTS"
	CodeUnsupportedMedia    = "UNSUPPORTED_MEDIA_TYPE"
	CodeInsufficientStorage = "INSUFFICIENT_STORAGE"
	CodeNotImplemented      = "NOT_IMPLEMENTED"
)

Standard error code constants for machine-readable error identification. Use these with WithCode() or rely on the factory defaults.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppError

type AppError struct {
	Status  int    // HTTP status code (e.g., 404, 500)
	Code    string // Machine-readable error code (e.g., "NOT_FOUND", "VALIDATION_FAILED")
	Message string // User-facing safe message
	Err     error  // Internal error (logged, never exposed in production)
}

AppError represents a structured application error with HTTP status context.

func BadRequest

func BadRequest(message string) *AppError

BadRequest creates a 400 error.

func Conflict

func Conflict(message string) *AppError

Conflict creates a 409 error.

func Forbidden

func Forbidden(message string) *AppError

Forbidden creates a 403 error.

func Internal

func Internal(err error) *AppError

Internal creates a 500 error wrapping an internal error.

func NotFound

func NotFound(message string) *AppError

NotFound creates a 404 error.

func RateLimited added in v2.7.1

func RateLimited(message string) *AppError

RateLimited creates a 429 error.

func ServiceUnavailable added in v2.7.1

func ServiceUnavailable(message string) *AppError

ServiceUnavailable creates a 503 error.

func Timeout added in v2.7.1

func Timeout(message string) *AppError

Timeout creates a 504 error.

func Unauthorized

func Unauthorized(message string) *AppError

Unauthorized creates a 401 error.

func Unprocessable

func Unprocessable(message string) *AppError

Unprocessable creates a 422 error.

func (*AppError) Error

func (e *AppError) Error() string

Error returns the user-facing message. Implements the error interface.

func (*AppError) ErrorResponse

func (e *AppError) ErrorResponse() map[string]any

ErrorResponse returns a map for JSON error responses. In debug mode, it includes internal error details. In production, only the safe message.

func (*AppError) HTTPStatus added in v2.5.0

func (e *AppError) HTTPStatus() int

HTTPStatus returns the HTTP status code. Deprecated: Access .Status directly instead.

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying error for errors.Is/As support.

func (*AppError) WithCode added in v2.5.0

func (e *AppError) WithCode(code string) *AppError

WithCode returns the error with a custom machine-readable code, overriding the default code set by the factory function.

Jump to

Keyboard shortcuts

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