errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

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)
	ErrInvalidToken     = New(ErrCodeInvalidToken, "invalid token", 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

func IsConflictError added in v0.1.5

func IsConflictError(err error) bool

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

func IsFormError added in v0.1.5

func IsFormError(err error) bool

func IsNotFound added in v0.1.5

func IsNotFound(err error) bool

Error type checking utilities

func IsSystemError added in v0.1.5

func IsSystemError(err error) bool

func IsUserError added in v0.1.5

func IsUserError(err error) bool

func IsValidation added in v0.1.5

func IsValidation(err error) bool

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 (
	// Validation errors
	ErrCodeValidation    ErrorCode = "VALIDATION_ERROR"
	ErrCodeRequired      ErrorCode = "REQUIRED_FIELD"
	ErrCodeInvalid       ErrorCode = "INVALID_VALUE"
	ErrCodeInvalidFormat ErrorCode = "INVALID_FORMAT"
	ErrCodeInvalidInput  ErrorCode = "INVALID_INPUT"

	// Authentication errors
	ErrCodeUnauthorized     ErrorCode = "UNAUTHORIZED"
	ErrCodeForbidden        ErrorCode = "FORBIDDEN"
	ErrCodeInvalidToken     ErrorCode = "INVALID_TOKEN"
	ErrCodeAuthentication   ErrorCode = "AUTHENTICATION_ERROR"
	ErrCodeInsufficientRole ErrorCode = "INSUFFICIENT_ROLE"

	// Resource errors
	ErrCodeNotFound      ErrorCode = "NOT_FOUND"
	ErrCodeConflict      ErrorCode = "CONFLICT"
	ErrCodeBadRequest    ErrorCode = "BAD_REQUEST"
	ErrCodeServerError   ErrorCode = "SERVER_ERROR"
	ErrCodeAlreadyExists ErrorCode = "ALREADY_EXISTS"

	// Application lifecycle errors
	ErrCodeStartup  ErrorCode = "STARTUP_ERROR"
	ErrCodeShutdown ErrorCode = "SHUTDOWN_ERROR"
	ErrCodeConfig   ErrorCode = "CONFIG_ERROR"
	ErrCodeDatabase ErrorCode = "DATABASE_ERROR"
	ErrCodeTimeout  ErrorCode = "TIMEOUT"

	// Form-specific errors
	ErrCodeFormValidation   ErrorCode = "FORM_VALIDATION_ERROR"
	ErrCodeFormNotFound     ErrorCode = "FORM_NOT_FOUND"
	ErrCodeFormSubmission   ErrorCode = "FORM_SUBMISSION_ERROR"
	ErrCodeFormAccessDenied ErrorCode = "FORM_ACCESS_DENIED"
	ErrCodeFormInvalid      ErrorCode = "FORM_INVALID"
	ErrCodeFormExpired      ErrorCode = "FORM_EXPIRED"

	// User-specific errors
	ErrCodeUserNotFound     ErrorCode = "USER_NOT_FOUND"
	ErrCodeUserExists       ErrorCode = "USER_EXISTS"
	ErrCodeUserDisabled     ErrorCode = "USER_DISABLED"
	ErrCodeUserInvalid      ErrorCode = "USER_INVALID"
	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

type HTTPError added in v0.2.0

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

HTTPError represents an HTTP error response

func NewHTTPError added in v0.2.0

func NewHTTPError(statusCode int, message string) *HTTPError

NewHTTPError creates a new HTTP error

func TranslateToHTTP added in v0.2.0

func TranslateToHTTP(err error) *HTTPError

TranslateToHTTP translates a domain error to an HTTP error

func (*HTTPError) Error added in v0.2.0

func (e *HTTPError) Error() string

Error implements the error interface

func (*HTTPError) WithDetails added in v0.2.0

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

WithDetails adds details to the HTTP error

Jump to

Keyboard shortcuts

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