errors

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineErrors

func CombineErrors(errors ...error) error

CombineErrors combines multiple errors into an error chain

func GetErrorCode

func GetErrorCode(err error) string

GetErrorCode returns the error code

func GetHTTPStatus

func GetHTTPStatus(err error) int

GetHTTPStatus returns the HTTP status code for an error

func IsCode

func IsCode(err error, code string) bool

IsCode checks if an error has a specific code

func IsRetryable

func IsRetryable(err error) bool

IsRetryable checks if an error is retryable

func IsType

func IsType(err error, errorType ErrorType) bool

IsType checks if an error is of a specific type

Types

type Error

type Error struct {
	Type       ErrorType              `json:"type"`
	Code       string                 `json:"code"`
	Message    string                 `json:"message"`
	Details    string                 `json:"details,omitempty"`
	Cause      error                  `json:"-"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
	Timestamp  time.Time              `json:"timestamp"`
	StackTrace []StackFrame           `json:"stack_trace,omitempty"`
	RequestID  string                 `json:"request_id,omitempty"`
	UserID     string                 `json:"user_id,omitempty"`
	Operation  string                 `json:"operation,omitempty"`
	Component  string                 `json:"component,omitempty"`
	Retryable  bool                   `json:"retryable"`
	HTTPStatus int                    `json:"http_status,omitempty"`
}

Error represents a structured error with additional context

func BadRequestError

func BadRequestError(code, message string) *Error

BadRequestError creates a bad request error

func ConflictError

func ConflictError(code, message string) *Error

ConflictError creates a conflict error

func ExternalError

func ExternalError(code, message string) *Error

ExternalError creates an external service error

func ForbiddenError

func ForbiddenError(code, message string) *Error

ForbiddenError creates a forbidden error

func InternalError

func InternalError(code, message string) *Error

InternalError creates an internal error

func NewError

func NewError(errorType ErrorType, code, message string) *Error

NewError creates a new structured error

func NotFoundError

func NotFoundError(code, message string) *Error

NotFoundError creates a not found error

func RateLimitError

func RateLimitError(code, message string) *Error

RateLimitError creates a rate limit error

func ServiceUnavailableError

func ServiceUnavailableError(code, message string) *Error

ServiceUnavailableError creates a service unavailable error

func TimeoutError

func TimeoutError(code, message string) *Error

TimeoutError creates a timeout error

func UnauthorizedError

func UnauthorizedError(code, message string) *Error

UnauthorizedError creates an unauthorized error

func ValidationError

func ValidationError(code, message string) *Error

ValidationError creates a validation error

func Wrap

func Wrap(err error, errorType ErrorType, code, message string) *Error

Wrap wraps an existing error with additional context

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Is

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

Is checks if the error matches the target error

func (*Error) JSON

func (e *Error) JSON() string

JSON returns the error as JSON

func (*Error) MarkRetryable

func (e *Error) MarkRetryable() *Error

MarkRetryable marks the error as retryable

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

WithCause adds a cause to the error

func (*Error) WithComponent

func (e *Error) WithComponent(component string) *Error

WithComponent adds a component name to the error

func (*Error) WithDetails

func (e *Error) WithDetails(details string) *Error

WithDetails adds details to the error

func (*Error) WithHTTPStatus

func (e *Error) WithHTTPStatus(status int) *Error

WithHTTPStatus adds an HTTP status code to the error

func (*Error) WithMetadata

func (e *Error) WithMetadata(key string, value interface{}) *Error

WithMetadata adds metadata to the error

func (*Error) WithOperation

func (e *Error) WithOperation(operation string) *Error

WithOperation adds an operation name to the error

func (*Error) WithRequestID

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

WithRequestID adds a request ID to the error

func (*Error) WithUserID

func (e *Error) WithUserID(userID string) *Error

WithUserID adds a user ID to the error

type ErrorChain

type ErrorChain struct {
	Errors []*Error `json:"errors"`
}

ErrorChain represents a chain of errors for aggregation

func NewErrorChain

func NewErrorChain() *ErrorChain

NewErrorChain creates a new error chain

func (*ErrorChain) Add

func (ec *ErrorChain) Add(err *Error)

Add adds an error to the chain

func (*ErrorChain) AddError

func (ec *ErrorChain) AddError(err error, errorType ErrorType, code, message string)

AddError adds a regular error to the chain by wrapping it

func (*ErrorChain) Count

func (ec *ErrorChain) Count() int

Count returns the number of errors in the chain

func (*ErrorChain) Error

func (ec *ErrorChain) Error() string

Error implements the error interface

func (*ErrorChain) Filter

func (ec *ErrorChain) Filter(errorType ErrorType) []*Error

Filter returns errors of a specific type

func (*ErrorChain) First

func (ec *ErrorChain) First() *Error

First returns the first error in the chain

func (*ErrorChain) HasErrors

func (ec *ErrorChain) HasErrors() bool

HasErrors returns true if there are any errors in the chain

func (*ErrorChain) JSON

func (ec *ErrorChain) JSON() string

JSON returns the error chain as JSON

func (*ErrorChain) Last

func (ec *ErrorChain) Last() *Error

Last returns the last error in the chain

type ErrorHandler

type ErrorHandler struct {
	DefaultComponent string
	Logger           func(error)
}

ErrorHandler provides utilities for error handling

func NewErrorHandler

func NewErrorHandler(component string, logger func(error)) *ErrorHandler

NewErrorHandler creates a new error handler

func (*ErrorHandler) Handle

func (eh *ErrorHandler) Handle(err error, sanitize bool) error

Handle handles an error by logging it and optionally returning a sanitized version

func (*ErrorHandler) Recover

func (eh *ErrorHandler) Recover() error

Recover recovers from panics and converts them to errors

func (*ErrorHandler) SafeExecute

func (eh *ErrorHandler) SafeExecute(fn func() error) (err error)

SafeExecute executes a function and handles any panics

type ErrorType

type ErrorType string

ErrorType represents different types of errors

const (
	ErrorTypeValidation         ErrorType = "validation"
	ErrorTypeNotFound           ErrorType = "not_found"
	ErrorTypeUnauthorized       ErrorType = "unauthorized"
	ErrorTypeForbidden          ErrorType = "forbidden"
	ErrorTypeConflict           ErrorType = "conflict"
	ErrorTypeInternal           ErrorType = "internal"
	ErrorTypeExternal           ErrorType = "external"
	ErrorTypeTimeout            ErrorType = "timeout"
	ErrorTypeRateLimit          ErrorType = "rate_limit"
	ErrorTypeBadRequest         ErrorType = "bad_request"
	ErrorTypeServiceUnavailable ErrorType = "service_unavailable"
)

func GetErrorType

func GetErrorType(err error) ErrorType

GetErrorType returns the error type

type StackFrame

type StackFrame struct {
	Function string `json:"function"`
	File     string `json:"file"`
	Line     int    `json:"line"`
}

StackFrame represents a stack frame

Jump to

Keyboard shortcuts

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