Documentation
¶
Overview ¶
Package errors provides enterprise-grade error handling with stack traces, error codes, and categorization.
Index ¶
- Constants
- func Handle(err error)
- func IsRetryable(err error) bool
- func SetGlobalErrorHandler(handler *ErrorHandler)
- type Error
- func FromStandardError(err error) *Error
- func New(code, message string) *Error
- func NewBusinessError(code, message string) *Error
- func NewCircuitOpenError(code, message string) *Error
- func NewConflictError(code, message string) *Error
- func NewDependencyError(code, message string) *Error
- func NewForbiddenError(code, message string) *Error
- func NewInternalError(code, message string) *Error
- func NewNotFoundError(code, message string) *Error
- func NewRateLimitError(code, message string) *Error
- func NewTimeoutError(code, message string) *Error
- func NewUnauthorizedError(code, message string) *Error
- func NewValidationError(code, message string) *Error
- func NewWithType(code, message string, errorType ErrorType) *Error
- func Wrap(err error, code, message string) *Error
- func Wrapf(err error, code, format string, args ...interface{}) *Error
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) ToJSON() ([]byte, error)
- func (e *Error) Unwrap() error
- func (e *Error) WithContext(key string, value interface{}) *Error
- func (e *Error) WithOperation(operation string) *Error
- func (e *Error) WithRequestID(requestID string) *Error
- func (e *Error) WithTenantID(tenantID string) *Error
- func (e *Error) WithUserID(userID string) *Error
- type ErrorChain
- type ErrorCollection
- type ErrorHandler
- type ErrorNotifier
- type ErrorType
- type HTTPErrorMapper
- type Severity
Constants ¶
const ( CodeUnknown = "UNKNOWN" CodeInvalidInput = "INVALID_INPUT" CodeMissingField = "MISSING_FIELD" CodeInvalidFormat = "INVALID_FORMAT" CodeResourceNotFound = "RESOURCE_NOT_FOUND" CodeUserNotFound = "USER_NOT_FOUND" CodeTenantNotFound = "TENANT_NOT_FOUND" CodeInvalidCredentials = "INVALID_CREDENTIALS" CodeTokenExpired = "TOKEN_EXPIRED" CodeTokenInvalid = "TOKEN_INVALID" CodeForbidden = "FORBIDDEN" CodeInsufficientPrivileges = "INSUFFICIENT_PRIVILEGES" CodeResourceConflict = "RESOURCE_CONFLICT" CodeDuplicateResource = "DUPLICATE_RESOURCE" CodeInternalError = "INTERNAL_ERROR" CodeDatabaseError = "DATABASE_ERROR" CodeExternalServiceError = "EXTERNAL_SERVICE_ERROR" CodeTimeout = "TIMEOUT" CodeRateLimitExceeded = "RATE_LIMIT_EXCEEDED" CodeCircuitBreakerOpen = "CIRCUIT_BREAKER_OPEN" CodeConfigurationError = "CONFIGURATION_ERROR" CodeBusinessRuleViolation = "BUSINESS_RULE_VIOLATION" )
Common Error Codes
Variables ¶
This section is empty.
Functions ¶
func SetGlobalErrorHandler ¶
func SetGlobalErrorHandler(handler *ErrorHandler)
SetGlobalErrorHandler sets the global error handler.
Types ¶
type Error ¶
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
Timestamp time.Time `json:"timestamp"`
StackTrace []string `json:"stack_trace,omitempty"`
Context map[string]interface{} `json:"context,omitempty"`
Cause error `json:"cause,omitempty"`
Type ErrorType `json:"type"`
Severity Severity `json:"severity"`
Retryable bool `json:"retryable"`
UserID string `json:"user_id,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
RequestID string `json:"request_id,omitempty"`
Operation string `json:"operation,omitempty"`
}
Error represents an enterprise error with additional context.
func FromStandardError ¶
FromStandardError converts a standard error to an enterprise error.
func NewBusinessError ¶
NewBusinessError creates a business logic error.
func NewCircuitOpenError ¶
NewCircuitOpenError creates a circuit breaker open error.
func NewConflictError ¶
NewConflictError creates a conflict error.
func NewDependencyError ¶
NewDependencyError creates a dependency error.
func NewForbiddenError ¶
NewForbiddenError creates a forbidden error.
func NewInternalError ¶
NewInternalError creates an internal server error.
func NewNotFoundError ¶
NewNotFoundError creates a not found error.
func NewRateLimitError ¶
NewRateLimitError creates a rate limit error.
func NewTimeoutError ¶
NewTimeoutError creates a timeout error.
func NewUnauthorizedError ¶
NewUnauthorizedError creates an unauthorized error.
func NewValidationError ¶
NewValidationError creates a validation error.
func NewWithType ¶
NewWithType creates a new error with specific type.
func (*Error) WithContext ¶
WithContext adds context to the error.
func (*Error) WithOperation ¶
WithOperation adds operation name to the error.
func (*Error) WithRequestID ¶
WithRequestID adds request ID to the error.
func (*Error) WithTenantID ¶
WithTenantID adds tenant ID to the error.
func (*Error) WithUserID ¶
WithUserID adds user ID to the error.
type ErrorChain ¶
type ErrorChain struct {
Errors []*Error `json:"errors"`
Root *Error `json:"root"`
Timestamp time.Time `json:"timestamp"`
}
ErrorChain represents a chain of related errors.
func NewErrorChain ¶
func NewErrorChain(root *Error) *ErrorChain
NewErrorChain creates a new error chain.
func (*ErrorChain) Add ¶
func (ec *ErrorChain) Add(err *Error) *ErrorChain
Add adds an error to the chain.
func (*ErrorChain) Error ¶
func (ec *ErrorChain) Error() string
Error implements the error interface.
func (*ErrorChain) Last ¶
func (ec *ErrorChain) Last() *Error
Last returns the last error in the chain.
type ErrorCollection ¶
type ErrorCollection struct {
Errors []*Error `json:"errors"`
Timestamp time.Time `json:"timestamp"`
}
ErrorCollection represents a collection of errors.
func NewErrorCollection ¶
func NewErrorCollection() *ErrorCollection
NewErrorCollection creates a new error collection.
func (*ErrorCollection) Add ¶
func (ec *ErrorCollection) Add(err *Error) *ErrorCollection
Add adds an error to the collection.
func (*ErrorCollection) Error ¶
func (ec *ErrorCollection) Error() string
Error implements the error interface.
func (*ErrorCollection) HasErrors ¶
func (ec *ErrorCollection) HasErrors() bool
HasErrors returns true if the collection has errors.
type ErrorHandler ¶
type ErrorHandler struct {
// contains filtered or unexported fields
}
ErrorHandler provides error handling utilities.
func NewErrorHandler ¶
func NewErrorHandler(notifier ErrorNotifier) *ErrorHandler
NewErrorHandler creates a new error handler.
func (*ErrorHandler) Handle ¶
func (eh *ErrorHandler) Handle(err error)
Handle handles an error with appropriate actions.
type ErrorNotifier ¶
ErrorNotifier defines the interface for error notification.
type ErrorType ¶
type ErrorType string
ErrorType categorizes errors by their nature.
const ( TypeValidation ErrorType = "validation" TypeNotFound ErrorType = "not_found" TypeForbidden ErrorType = "forbidden" TypeConflict ErrorType = "conflict" TypeInternal ErrorType = "internal" TypeExternal ErrorType = "external" TypeTimeout ErrorType = "timeout" TypeRateLimit ErrorType = "rate_limit" TypeCircuitOpen ErrorType = "circuit_open" TypeDependency ErrorType = "dependency" TypeConfiguration ErrorType = "configuration" TypeBusiness ErrorType = "business" )
type HTTPErrorMapper ¶
type HTTPErrorMapper struct{}
HTTPErrorMapper maps enterprise errors to HTTP status codes.
func (*HTTPErrorMapper) MapToHTTPResponse ¶
func (m *HTTPErrorMapper) MapToHTTPResponse(err error) map[string]interface{}
MapToHTTPResponse maps an enterprise error to HTTP response body.
func (*HTTPErrorMapper) MapToHTTPStatus ¶
func (m *HTTPErrorMapper) MapToHTTPStatus(err error) int
MapToHTTPStatus maps an enterprise error to HTTP status code.