errors

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeUnauthorized       = "UNAUTHORIZED"
	CodeForbidden          = "FORBIDDEN"
	CodeNotFound           = "NOT_FOUND"
	CodeBadRequest         = "BAD_REQUEST"
	CodeInternalError      = "INTERNAL_ERROR"
	CodeValidation         = "VALIDATION_ERROR"
	CodeConflict           = "CONFLICT"
	CodeRateLimited        = "RATE_LIMITED"
	CodeTokenExpired       = "TOKEN_EXPIRED"
	CodeTokenInvalid       = "TOKEN_INVALID"
	CodeInsufficientRole   = "INSUFFICIENT_ROLE"
	CodeResourceNotOwned   = "RESOURCE_NOT_OWNED"
	CodeDatabaseError      = "DATABASE_ERROR"
	CodeConfigurationError = "CONFIGURATION_ERROR"
)

Standard error codes

Variables

View Source
var (
	ErrUnauthorized       = NewUnauthorizedError("authentication required")
	ErrForbidden          = NewForbiddenError("access denied")
	ErrTokenExpired       = NewTokenExpiredError()
	ErrInvalidCredentials = NewUnauthorizedError("invalid credentials")
	ErrRateLimited        = NewRateLimitedError()
)

Sentinel errors for common cases

Functions

func GetErrorCode

func GetErrorCode(err error) string

GetErrorCode returns the error code for an error

func GetHTTPStatus

func GetHTTPStatus(err error) int

GetHTTPStatus returns the HTTP status code for an error

func IsAppError

func IsAppError(err error) bool

IsAppError checks if an error is an AppError

Types

type AppError

type AppError struct {
	// Code is the error code for programmatic handling
	Code string `json:"code"`

	// Message is the human-readable error message
	Message string `json:"message"`

	// HTTPStatus is the HTTP status code to return
	HTTPStatus int `json:"-"`

	// Op is the operation that failed
	Op string `json:"-"`

	// Err is the underlying error
	Err error `json:"-"`

	// Details contains additional error context
	Details map[string]interface{} `json:"details,omitempty"`
}

AppError represents an application error with context

func GetAppError

func GetAppError(err error) *AppError

GetAppError extracts AppError from an error chain

func NewBadRequestError

func NewBadRequestError(message string) *AppError

NewBadRequestError creates a bad request error

func NewConfigurationError

func NewConfigurationError(message string) *AppError

NewConfigurationError creates a configuration error

func NewConflictError

func NewConflictError(message string) *AppError

NewConflictError creates a conflict error

func NewDatabaseError

func NewDatabaseError(op string, err error) *AppError

NewDatabaseError creates a database error

func NewForbiddenError

func NewForbiddenError(message string) *AppError

NewForbiddenError creates a forbidden error

func NewInsufficientRoleError

func NewInsufficientRoleError(required, actual string) *AppError

NewInsufficientRoleError creates an insufficient role error

func NewInternalError

func NewInternalError(message string) *AppError

NewInternalError creates an internal server error

func NewNotFoundError

func NewNotFoundError(resource string) *AppError

NewNotFoundError creates a not found error

func NewRateLimitedError

func NewRateLimitedError() *AppError

NewRateLimitedError creates a rate limited error

func NewTokenExpiredError

func NewTokenExpiredError() *AppError

NewTokenExpiredError creates a token expired error

func NewTokenInvalidError

func NewTokenInvalidError(reason string) *AppError

NewTokenInvalidError creates a token invalid error

func NewUnauthorizedError

func NewUnauthorizedError(message string) *AppError

NewUnauthorizedError creates an unauthorized error

func NewValidationError

func NewValidationError(message string) *AppError

NewValidationError creates a validation error

func (*AppError) Error

func (e *AppError) Error() string

Error implements the error interface

func (*AppError) Is

func (e *AppError) Is(target error) bool

Is checks if the error matches the target

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying error

func (*AppError) WithDetails

func (e *AppError) WithDetails(key string, value interface{}) *AppError

WithDetails adds additional context details

func (*AppError) WithOp

func (e *AppError) WithOp(op string) *AppError

WithOp adds operation context to the error

func (*AppError) Wrap

func (e *AppError) Wrap(err error) *AppError

Wrap wraps an underlying error

Jump to

Keyboard shortcuts

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