errors

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides domain-specific error types and utilities for consistent error handling across the application.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Validation errors
	ErrValidation    = New(ErrCodeValidation, "validation error", nil)
	ErrRequiredField = New(ErrCodeRequired, "field is required", nil)
	ErrInvalidFormat = New(ErrCodeInvalidFormat, "invalid format", nil)
	ErrInvalidValue  = New(ErrCodeInvalid, "invalid value", nil)
	ErrInvalidInput  = New(ErrCodeInvalidInput, "invalid input", nil)

	// Authentication errors
	ErrUnauthorized     = New(ErrCodeUnauthorized, "unauthorized", nil)
	ErrForbidden        = New(ErrCodeForbidden, "forbidden", nil)
	ErrAuthentication   = New(ErrCodeAuthentication, "authentication error", nil)
	ErrInsufficientRole = New(ErrCodeInsufficientRole, "insufficient role", nil)

	// Resource errors
	ErrNotFound      = New(ErrCodeNotFound, "resource not found", nil)
	ErrConflict      = New(ErrCodeConflict, "resource conflict", nil)
	ErrBadRequest    = New(ErrCodeBadRequest, "bad request", nil)
	ErrServerError   = New(ErrCodeServerError, "internal server error", nil)
	ErrAlreadyExists = New(ErrCodeAlreadyExists, "resource already exists", nil)

	// System errors
	ErrDatabase = New(ErrCodeDatabase, "database error", nil)
	ErrTimeout  = New(ErrCodeTimeout, "operation timed out", nil)
	ErrConfig   = New(ErrCodeConfig, "configuration error", nil)

	// Form-specific errors
	ErrFormValidation   = New(ErrCodeFormValidation, "form validation error", nil)
	ErrFormNotFound     = New(ErrCodeFormNotFound, "form not found", nil)
	ErrFormSubmission   = New(ErrCodeFormSubmission, "form submission error", nil)
	ErrFormAccessDenied = New(ErrCodeFormAccessDenied, "form access denied", nil)
	ErrFormInvalid      = New(ErrCodeFormInvalid, "invalid form", nil)
	ErrFormExpired      = New(ErrCodeFormExpired, "form has expired", nil)

	// User-specific errors
	ErrUserNotFound     = New(ErrCodeUserNotFound, "user not found", nil)
	ErrUserExists       = New(ErrCodeUserExists, "user already exists", nil)
	ErrUserDisabled     = New(ErrCodeUserDisabled, "user is disabled", nil)
	ErrUserInvalid      = New(ErrCodeUserInvalid, "invalid user", nil)
	ErrUserUnauthorized = New(ErrCodeUserUnauthorized, "user is not authorized", nil)
)

Common error instances

Functions

func GetErrorContext added in v0.1.5

func GetErrorContext(err error) map[string]any

GetErrorContext returns the error context if the error is a DomainError

func GetErrorDetails added in v0.1.5

func GetErrorDetails(err error) map[string]any

GetErrorDetails returns the error details if the error is a DomainError

func GetErrorMessage added in v0.1.5

func GetErrorMessage(err error) string

GetErrorMessage returns the error message

func GetErrorStack added in v0.1.5

func GetErrorStack(err error) []error

GetErrorStack returns the error stack if the error is a DomainError

func GetFullErrorMessage added in v0.1.5

func GetFullErrorMessage(err error) string

GetFullErrorMessage returns the full error message including wrapped errors

func GetHTTPStatus added in v0.1.5

func GetHTTPStatus(code ErrorCode) int

GetHTTPStatus returns the appropriate HTTP status code for an error code

func IsAuthenticationError added in v0.1.5

func IsAuthenticationError(err error) bool

IsAuthenticationError checks if the error represents an authentication error

func IsConflictError added in v0.1.5

func IsConflictError(err error) bool

IsConflictError checks if the error represents a conflict error

func IsDomainError added in v0.1.5

func IsDomainError(err error) bool

IsDomainError checks if the error is a domain error

func IsForbiddenError added in v0.1.5

func IsForbiddenError(err error) bool

IsForbiddenError checks if the error represents a forbidden error

func IsFormError added in v0.1.5

func IsFormError(err error) bool

IsFormError checks if the error represents a form-related error

func IsNotFound added in v0.1.5

func IsNotFound(err error) bool

IsNotFound checks if the error represents a "not found" error

func IsSystemError added in v0.1.5

func IsSystemError(err error) bool

IsSystemError checks if the error represents a system error

func IsUserError added in v0.1.5

func IsUserError(err error) bool

IsUserError checks if the error represents a user-related error

func IsValidation added in v0.1.5

func IsValidation(err error) bool

IsValidation checks if the error represents a validation error

func UnwrapError added in v0.1.5

func UnwrapError(err error) error

UnwrapError unwraps an error to its original error

func WrapAuthenticationError added in v0.1.5

func WrapAuthenticationError(err error, message string) error

WrapAuthenticationError wraps an error with an authentication error

func WrapAuthorizationError added in v0.1.5

func WrapAuthorizationError(err error, message string) error

WrapAuthorizationError wraps an error with an authorization error

func WrapError added in v0.1.5

func WrapError(err error, code ErrorCode, message string) error

WrapError wraps an error with a domain error

func WrapNotFoundError added in v0.1.5

func WrapNotFoundError(err error, message string) error

WrapNotFoundError wraps an error with a not found error

func WrapValidationError added in v0.1.5

func WrapValidationError(err error, message string) error

WrapValidationError wraps an error with a validation error

Types

type DomainError

type DomainError struct {
	Code    ErrorCode
	Message string
	Err     error
	Context map[string]any
}

DomainError represents a domain-specific error

func GetDomainError added in v0.1.5

func GetDomainError(err error) *DomainError

GetDomainError returns the domain error if the error is a DomainError

func New

func New(code ErrorCode, message string, err error) *DomainError

New creates a new domain error

func Wrap

func Wrap(err error, code ErrorCode, message string) *DomainError

Wrap wraps an existing error with domain context

func WrapErrorf added in v0.1.5

func WrapErrorf(err error, code ErrorCode, format string, args ...any) *DomainError

WrapErrorf wraps an error with a formatted message

func (*DomainError) Error

func (e *DomainError) Error() string

func (*DomainError) HTTPStatus added in v0.1.5

func (e *DomainError) HTTPStatus() int

HTTPStatus returns the appropriate HTTP status code for the error

func (*DomainError) ToResponse added in v0.1.5

func (e *DomainError) ToResponse() ErrorResponse

ToResponse converts the DomainError to a standardized ErrorResponse

func (*DomainError) Unwrap

func (e *DomainError) Unwrap() error

func (*DomainError) WithContext

func (e *DomainError) WithContext(key string, value any) *DomainError

WithContext adds context to the error

type ErrorCode

type ErrorCode string

ErrorCode represents a specific type of error

const (
	// ErrCodeValidation represents a validation error
	ErrCodeValidation ErrorCode = "VALIDATION_ERROR"
	// ErrCodeRequired represents a required field error
	ErrCodeRequired ErrorCode = "REQUIRED_FIELD"
	// ErrCodeInvalid represents an invalid value error
	ErrCodeInvalid ErrorCode = "INVALID_VALUE"
	// ErrCodeInvalidFormat represents an invalid format error
	ErrCodeInvalidFormat ErrorCode = "INVALID_FORMAT"
	// ErrCodeInvalidInput represents an invalid input error
	ErrCodeInvalidInput ErrorCode = "INVALID_INPUT"

	// ErrCodeUnauthorized represents an unauthorized access error
	ErrCodeUnauthorized ErrorCode = "UNAUTHORIZED"
	// ErrCodeForbidden represents a forbidden access error
	ErrCodeForbidden ErrorCode = "FORBIDDEN"
	// ErrCodeAuthentication represents an authentication error
	ErrCodeAuthentication ErrorCode = "AUTHENTICATION_ERROR"
	// ErrCodeInsufficientRole represents an insufficient role error
	ErrCodeInsufficientRole ErrorCode = "INSUFFICIENT_ROLE"

	// ErrCodeNotFound represents a resource not found error
	ErrCodeNotFound ErrorCode = "NOT_FOUND"
	// ErrCodeConflict represents a resource conflict error
	ErrCodeConflict ErrorCode = "CONFLICT"
	// ErrCodeBadRequest represents a bad request error
	ErrCodeBadRequest ErrorCode = "BAD_REQUEST"
	// ErrCodeServerError represents a server error
	ErrCodeServerError ErrorCode = "SERVER_ERROR"
	// ErrCodeAlreadyExists represents a resource already exists error
	ErrCodeAlreadyExists ErrorCode = "ALREADY_EXISTS"

	// ErrCodeStartup represents a startup error
	ErrCodeStartup ErrorCode = "STARTUP_ERROR"
	// ErrCodeShutdown represents a shutdown error
	ErrCodeShutdown ErrorCode = "SHUTDOWN_ERROR"
	// ErrCodeConfig represents a configuration error
	ErrCodeConfig ErrorCode = "CONFIG_ERROR"
	// ErrCodeDatabase represents a database error
	ErrCodeDatabase ErrorCode = "DATABASE_ERROR"
	// ErrCodeTimeout represents a timeout error
	ErrCodeTimeout ErrorCode = "TIMEOUT"

	// ErrCodeFormValidation represents a form validation error
	ErrCodeFormValidation ErrorCode = "FORM_VALIDATION_ERROR"
	// ErrCodeFormNotFound represents a form not found error
	ErrCodeFormNotFound ErrorCode = "FORM_NOT_FOUND"
	// ErrCodeFormSubmission represents a form submission error
	ErrCodeFormSubmission ErrorCode = "FORM_SUBMISSION_ERROR"
	// ErrCodeFormAccessDenied represents a form access denied error
	ErrCodeFormAccessDenied ErrorCode = "FORM_ACCESS_DENIED"
	// ErrCodeFormInvalid represents an invalid form error
	ErrCodeFormInvalid ErrorCode = "FORM_INVALID"
	// ErrCodeFormExpired represents a form expired error
	ErrCodeFormExpired ErrorCode = "FORM_EXPIRED"

	// ErrCodeUserNotFound represents a user not found error
	ErrCodeUserNotFound ErrorCode = "USER_NOT_FOUND"
	// ErrCodeUserExists represents a user already exists error
	ErrCodeUserExists ErrorCode = "USER_EXISTS"
	// ErrCodeUserDisabled represents a user disabled error
	ErrCodeUserDisabled ErrorCode = "USER_DISABLED"
	// ErrCodeUserInvalid represents an invalid user error
	ErrCodeUserInvalid ErrorCode = "USER_INVALID"
	// ErrCodeUserUnauthorized represents a user unauthorized error
	ErrCodeUserUnauthorized ErrorCode = "USER_UNAUTHORIZED"
)

func GetErrorCode added in v0.1.5

func GetErrorCode(err error) ErrorCode

GetErrorCode returns the error code if the error is a DomainError

type ErrorResponse added in v0.1.5

type ErrorResponse struct {
	Code    string         `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details,omitempty"`
}

ErrorResponse represents a standardized error response for HTTP handlers

Jump to

Keyboard shortcuts

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