Documentation
¶
Index ¶
- Constants
- func ErrorRecover() (err error)
- func ErrorRecoverWithHandler(handler func(*AppError))
- type AppError
- func FromError(err error) *AppError
- func New(errType ErrorType, message string) *AppError
- func NewBusiness(message string) *AppError
- func NewConflict(resource string, id interface{}) *AppError
- func NewDatabase(message string) *AppError
- func NewExternal(message string) *AppError
- func NewForbidden(message string) *AppError
- func NewInternal(message string) *AppError
- func NewInvalid(field string, value interface{}, reason string) *AppError
- func NewNotFound(resource string, id interface{}) *AppError
- func NewRateLimit(message string) *AppError
- func NewRequired(field string) *AppError
- func NewTimeout(message string) *AppError
- func NewUnauthorized(message string) *AppError
- func NewValidation(message string) *AppError
- func Wrap(err error, message string) *AppError
- func WrapWithType(err error, errType ErrorType, message string) *AppError
- func (e *AppError) Error() string
- func (e *AppError) Is(target error) bool
- func (e *AppError) Unwrap() error
- func (e *AppError) WithCode(code string) *AppError
- func (e *AppError) WithDetail(key string, value interface{}) *AppError
- func (e *AppError) WithDetails(details map[string]interface{}) *AppError
- func (e *AppError) WithHTTPStatus(status int) *AppError
- func (e *AppError) WithInnerError(err error) *AppError
- func (e *AppError) WithMessage(msg string) *AppError
- func (e *AppError) WithStack() *AppError
- type ErrorChain
- func (c *ErrorChain) Add(err *AppError) *ErrorChain
- func (c *ErrorChain) Error() string
- func (c *ErrorChain) Errors() []*AppError
- func (c *ErrorChain) Filter(errType ErrorType) *ErrorChain
- func (c *ErrorChain) First() *AppError
- func (c *ErrorChain) HasErrors() bool
- func (c *ErrorChain) HasType(errType ErrorType) bool
- func (c *ErrorChain) Last() *AppError
- func (c *ErrorChain) ToHTTPStatus() int
- type ErrorConverter
- type ErrorFormatter
- type ErrorHandler
- type ErrorLogger
- type ErrorLoggerInterface
- type ErrorRegistry
- type ErrorResponse
- type ErrorRetryer
- type ErrorType
- type ErrorValidator
- type HTTPErrorResponse
Constants ¶
const ( CodeValidationFailed = "VALIDATION_FAILED" CodeRequiredField = "REQUIRED_FIELD" CodeInvalidField = "INVALID_FIELD" CodeNotFound = "NOT_FOUND" CodeConflict = "CONFLICT" CodeForbidden = "FORBIDDEN" CodeRateLimit = "RATE_LIMIT" CodeInternalError = "INTERNAL_ERROR" )
Error codes for specific scenarios
Variables ¶
This section is empty.
Functions ¶
func ErrorRecover ¶
func ErrorRecover() (err error)
ErrorRecover recovers from panics and converts them to errors
func ErrorRecoverWithHandler ¶
func ErrorRecoverWithHandler(handler func(*AppError))
ErrorRecoverWithHandler recovers from panics and handles them
Types ¶
type AppError ¶
type AppError struct {
Type ErrorType `json:"type"`
Code string `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details,omitempty"`
InnerError error `json:"-"`
Stack []string `json:"-"`
HTTPStatus int `json:"-"`
}
AppError represents a structured application error
func NewConflict ¶
func NewExternal ¶
func NewForbidden ¶
func NewInvalid ¶
func NewNotFound ¶
func NewRateLimit ¶
func NewRequired ¶
func NewTimeout ¶
func WrapWithType ¶
WrapWithType wraps an error with a specific type
func (*AppError) WithDetail ¶
WithDetail adds a detail to the error
func (*AppError) WithDetails ¶
WithDetails adds multiple details to the error
func (*AppError) WithHTTPStatus ¶
WithHTTPStatus sets the HTTP status code
func (*AppError) WithInnerError ¶
WithInnerError sets the inner error
func (*AppError) WithMessage ¶
WithMessage adds a message to the error
type ErrorChain ¶
type ErrorChain struct {
// contains filtered or unexported fields
}
ErrorChain represents a chain of errors
func (*ErrorChain) Add ¶
func (c *ErrorChain) Add(err *AppError) *ErrorChain
Add adds an error to the chain
func (*ErrorChain) Error ¶
func (c *ErrorChain) Error() string
Error returns the combined error message
func (*ErrorChain) Errors ¶
func (c *ErrorChain) Errors() []*AppError
Errors returns all errors in the chain
func (*ErrorChain) Filter ¶
func (c *ErrorChain) Filter(errType ErrorType) *ErrorChain
Filter filters errors by type
func (*ErrorChain) First ¶
func (c *ErrorChain) First() *AppError
First returns the first error in the chain
func (*ErrorChain) HasErrors ¶
func (c *ErrorChain) HasErrors() bool
HasErrors checks if the chain has errors
func (*ErrorChain) HasType ¶
func (c *ErrorChain) HasType(errType ErrorType) bool
HasType checks if the chain has an error of the specified type
func (*ErrorChain) Last ¶
func (c *ErrorChain) Last() *AppError
Last returns the last error in the chain
func (*ErrorChain) ToHTTPStatus ¶
func (c *ErrorChain) ToHTTPStatus() int
ToHTTPStatus converts the error chain to an HTTP status code
type ErrorConverter ¶
type ErrorConverter struct {
// contains filtered or unexported fields
}
ErrorConverter converts errors to HTTP responses
func NewErrorConverter ¶
func NewErrorConverter(errorHandler *ErrorHandler) *ErrorConverter
NewErrorConverter creates a new error converter
func (*ErrorConverter) ToHTTPResponse ¶
func (c *ErrorConverter) ToHTTPResponse(err error) HTTPErrorResponse
ToHTTPResponse converts an error to an HTTP response
type ErrorFormatter ¶
type ErrorFormatter struct {
// contains filtered or unexported fields
}
ErrorFormatter formats errors for display
func NewErrorFormatter ¶
func NewErrorFormatter(showStack bool, showInner bool) *ErrorFormatter
NewErrorFormatter creates a new error formatter
func (*ErrorFormatter) Format ¶
func (f *ErrorFormatter) Format(err error) string
Format formats an error as a string
type ErrorHandler ¶
type ErrorHandler struct {
// contains filtered or unexported fields
}
ErrorHandler handles errors in a standardized way
func NewErrorHandler ¶
func NewErrorHandler(registry *ErrorRegistry) *ErrorHandler
NewErrorHandler creates a new error handler
func (*ErrorHandler) Handle ¶
func (h *ErrorHandler) Handle(err error) *AppError
Handle handles an error
func (*ErrorHandler) HandleFunc ¶
func (h *ErrorHandler) HandleFunc(errType ErrorType, fn func(*AppError) *AppError)
HandleFunc registers a handler for a specific error type
type ErrorLogger ¶
type ErrorLogger struct {
// contains filtered or unexported fields
}
ErrorLogger logs errors with context
func NewErrorLogger ¶
func NewErrorLogger(logger ErrorLoggerInterface) *ErrorLogger
NewErrorLogger creates a new error logger
func (*ErrorLogger) Log ¶
func (l *ErrorLogger) Log(err error, context map[string]interface{})
Log logs an error with context
type ErrorLoggerInterface ¶
type ErrorLoggerInterface interface {
Error(msg string, fields ...interface{})
Errorf(format string, args ...interface{})
WithField(key string, value interface{}) interface{}
}
ErrorLoggerInterface defines the interface for logging
type ErrorRegistry ¶
type ErrorRegistry struct {
// contains filtered or unexported fields
}
ErrorRegistry manages error definitions
func DefaultErrorRegistry ¶
func DefaultErrorRegistry() *ErrorRegistry
DefaultErrorRegistry creates a default error registry with common errors
func NewErrorRegistry ¶
func NewErrorRegistry() *ErrorRegistry
NewErrorRegistry creates a new error registry
func (*ErrorRegistry) Create ¶
func (r *ErrorRegistry) Create(code string, details map[string]interface{}) *AppError
Create creates a new error from a registered template
func (*ErrorRegistry) Get ¶
func (r *ErrorRegistry) Get(code string) *AppError
Get retrieves a registered error template
func (*ErrorRegistry) Register ¶
func (r *ErrorRegistry) Register(code string, err *AppError)
Register registers an error template
type ErrorResponse ¶
type ErrorResponse struct {
Type string `json:"type"`
Code string `json:"code"`
Message string `json:"message"`
Details map[string]interface{} `json:"details,omitempty"`
}
ErrorResponse represents the error part of an HTTP response
type ErrorRetryer ¶
type ErrorRetryer struct {
// contains filtered or unexported fields
}
ErrorRetryer retries operations that may fail
func NewErrorRetryer ¶
func NewErrorRetryer(maxAttempts int) *ErrorRetryer
NewErrorRetryer creates a new error retryer
func (*ErrorRetryer) Do ¶
func (r *ErrorRetryer) Do(fn func() error) error
Do executes a function with retry logic
func (*ErrorRetryer) WithRetryDelay ¶
func (r *ErrorRetryer) WithRetryDelay(fn func(int) int64) *ErrorRetryer
WithRetryDelay sets the retry delay function
func (*ErrorRetryer) WithRetryable ¶
func (r *ErrorRetryer) WithRetryable(fn func(error) bool) *ErrorRetryer
WithRetryable sets the retryable function
type ErrorType ¶
type ErrorType string
ErrorType represents the type of error
const ( // Validation errors ErrorTypeValidation ErrorType = "validation" ErrorTypeRequired ErrorType = "required" ErrorTypeInvalid ErrorType = "invalid" // Database errors ErrorTypeDatabase ErrorType = "database" ErrorTypeNotFound ErrorType = "not_found" ErrorTypeConflict ErrorType = "conflict" // Authorization errors ErrorTypeForbidden ErrorType = "forbidden" // Business errors ErrorTypeBusiness ErrorType = "business" ErrorTypeRateLimit ErrorType = "rate_limit" ErrorTypeTimeout ErrorType = "timeout" // System errors ErrorTypeInternal ErrorType = "internal" ErrorTypeExternal ErrorType = "external" ErrorTypeUnknown ErrorType = "unknown" )
type ErrorValidator ¶
type ErrorValidator struct {
// contains filtered or unexported fields
}
ErrorValidator validates errors
func NewErrorValidator ¶
func NewErrorValidator(allowedTypes ...ErrorType) *ErrorValidator
NewErrorValidator creates a new error validator
func (*ErrorValidator) IsAllowed ¶
func (v *ErrorValidator) IsAllowed(err error) bool
IsAllowed checks if an error type is allowed
func (*ErrorValidator) Validate ¶
func (v *ErrorValidator) Validate(err error) error
Validate validates an error against rules
type HTTPErrorResponse ¶
type HTTPErrorResponse struct {
HTTPStatus int `json:"-"`
Error ErrorResponse `json:"error"`
}
HTTPErrorResponse represents an HTTP error response