Documentation
¶
Index ¶
- Constants
- Variables
- func HTTPStatusToTextCode(code int) string
- func HasCategory(err error, category Category) bool
- func IsAuth(err error) bool
- func IsCategory(err error, category Category) bool
- func IsCommand(err error) bool
- func IsInternal(err error) bool
- func IsLocationCaptureEnabled() bool
- func IsNotFound(err error) bool
- func IsRetryableError(err error) bool
- func IsValidation(err error) bool
- func IsWrapped(err error) bool
- func LogBySeverity(logger *slog.Logger, err *Error)
- func RootCause(err error) error
- func SetLocationCapture(enabled bool)
- func ToSlogAttributes(err error) []slog.Attr
- type BaseError
- type Category
- type CollectorOption
- type Error
- func FromOzzoValidation(err error, message string) *Error
- func MapAuthErrors(err error) *Error
- func MapHTTPErrors(err error) *Error
- func MapToError(err error, mappers []ErrorMapper) *Error
- func New(message string, category ...Category) *Error
- func NewCritical(message string, category Category) *Error
- func NewDebug(message string, category Category) *Error
- func NewInfo(message string, category Category) *Error
- func NewValidation(message string, fieldErrors ...FieldError) *Error
- func NewValidationFromGroups(message string, groups map[string][]string) *Error
- func NewValidationFromMap(message string, fieldMap map[string]string) *Error
- func NewWarning(message string, category Category) *Error
- func NewWithLocation(message string, category Category, location *ErrorLocation) *Error
- func ValidateWithOzzo(validateFunc func() error, message string) *Error
- func Wrap(source error, category Category, message string) *Error
- func (e *Error) AllValidationErrors() ValidationErrors
- func (e *Error) Clone() *Error
- func (e *Error) Error() string
- func (e *Error) ErrorWithStack() string
- func (e *Error) GetLocation() *ErrorLocation
- func (e *Error) GetSeverity() Severity
- func (e *Error) HasLocation() bool
- func (e *Error) HasSeverity(s Severity) bool
- func (e *Error) IsAboveSeverity(threshold Severity) bool
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) ToErrorResponse(includeStack bool, stackTrace StackTrace) ErrorResponse
- func (e *Error) Unwrap() error
- func (e *Error) ValidationMap() map[string]string
- func (e *Error) WithCode(code int) *Error
- func (e *Error) WithLocation(loc *ErrorLocation) *Error
- func (e *Error) WithMetadata(metas ...map[string]any) *Error
- func (e *Error) WithRequestID(id string) *Error
- func (e *Error) WithSeverity(s Severity) *Error
- func (e *Error) WithStackTrace() *Error
- func (e *Error) WithTextCode(code string) *Error
- type ErrorCollector
- func (c *ErrorCollector) Add(err error) bool
- func (c *ErrorCollector) AddFieldErrors(errors ...FieldError)
- func (c *ErrorCollector) AddRetryable(err error, category Category, message string)
- func (c *ErrorCollector) AddValidation(field, message string)
- func (c *ErrorCollector) CategoryStats() map[Category]int
- func (c *ErrorCollector) Count() int
- func (c *ErrorCollector) Errors() []*Error
- func (c *ErrorCollector) FilterByCategory(cat Category) []*Error
- func (c *ErrorCollector) FilterBySeverity(min Severity) []*Error
- func (c *ErrorCollector) GetAllValidationErrors() ValidationErrors
- func (c *ErrorCollector) GetRetryableErrors() []*Error
- func (c *ErrorCollector) GetValidationErrors() ValidationErrors
- func (c *ErrorCollector) HasErrors() bool
- func (c *ErrorCollector) HasRetryableErrors() bool
- func (c *ErrorCollector) LogErrors(logger *slog.Logger)
- func (c *ErrorCollector) Merge() *Error
- func (c *ErrorCollector) MostCommonCategory() Category
- func (c *ErrorCollector) Reset()
- func (c *ErrorCollector) SeverityDistribution() map[Severity]int
- func (c *ErrorCollector) ToErrorResponse(includeStack bool) *ErrorResponse
- func (c *ErrorCollector) ToSlogAttributes() []slog.Attr
- type ErrorLocation
- type ErrorMapper
- type ErrorResponse
- type FieldError
- type LocationConfig
- type RetryableError
- func NewNonRetryable(message string, category Category) *RetryableError
- func NewRetryable(message string, category Category) *RetryableError
- func NewRetryableExternal(message string) *RetryableError
- func NewRetryableOperation(message string, delay ...time.Duration) *RetryableError
- func WrapRetryable(source error, category Category, message string) *RetryableError
- func (r *RetryableError) Error() string
- func (r *RetryableError) IsRetryable() bool
- func (r *RetryableError) RetryDealy(attempt int) time.Duration
- func (r *RetryableError) WithCode(code int) *RetryableError
- func (r *RetryableError) WithLocation(loc *ErrorLocation) *RetryableError
- func (r *RetryableError) WithMetadata(metas ...map[string]any) *RetryableError
- func (r *RetryableError) WithRetryDelay(delay time.Duration) *RetryableError
- func (r *RetryableError) WithRetryable(retryable bool) *RetryableError
- func (r *RetryableError) WithSeverity(s Severity) *RetryableError
- func (r *RetryableError) WithStackTrace() *RetryableError
- func (r *RetryableError) WithTextCode(code string) *RetryableError
- type Severity
- type StackFrame
- type StackTrace
- type ValidationErrors
Constants ¶
const ( CodeNotFound = http.StatusNotFound CodeConflict = http.StatusConflict CodeBadRequest = http.StatusBadRequest CodeForbidden = http.StatusForbidden CodeInternal = http.StatusInternalServerError CodeRequestTimeout = http.StatusRequestTimeout CodeTooManyRequests = http.StatusTooManyRequests )
Variables ¶
var ( Verbose = false IsDevelopment = false )
Global behavior
var ( // EnableLocationCapture controls whether location information is captured for new errors // This can be disabled in production for performance optimization EnableLocationCapture = true )
Global configuration for location capture
Functions ¶
func HTTPStatusToTextCode ¶ added in v0.3.0
HTTPStatusToTextCode generates text codes from HTTP status codes
func HasCategory ¶ added in v0.8.0
TODO: Should this be how IsCategory actually functions?!
func IsCategory ¶
func IsInternal ¶
func IsLocationCaptureEnabled ¶ added in v0.9.0
func IsLocationCaptureEnabled() bool
IsLocationCaptureEnabled returns whether location capture is currently enabled
func IsNotFound ¶
func IsRetryableError ¶ added in v0.5.0
IsRetryableError checks if an error implements the IsRetryable interface and returns true
func IsValidation ¶
func IsWrapped ¶ added in v0.5.1
IsWrapped checks if an error is already wrapped by our custom error types
func LogBySeverity ¶ added in v0.9.0
LogBySeverity logs an error using the appropriate slog level based on its severity
func SetLocationCapture ¶ added in v0.9.0
func SetLocationCapture(enabled bool)
SetLocationCapture globally enables or disables location capture
func ToSlogAttributes ¶ added in v0.8.0
Types ¶
type Category ¶
type Category string
Category represents a high level error category
const ( CategoryValidation Category = "validation" CategoryAuth Category = "authentication" CategoryAuthz Category = "authorization" CategoryOperation Category = "operation" CategoryNotFound Category = "not_found" CategoryConflict Category = "conflict" CategoryRateLimit Category = "rate_limit" CategoryBadInput Category = "bad_input" CategoryInternal Category = "internal" CategoryExternal Category = "external" CategoryMiddleware Category = "middleware" CategoryRouting Category = "routing" CategoryHandler Category = "handler" CategoryMethodNotAllowed Category = "method_not_allowed" CategoryCommand Category = "command" )
func HTTPStatusToCategory ¶ added in v0.3.0
HTTPStatusToCategory maps HTTP status codes to error categories
func RootCategory ¶ added in v0.8.0
type CollectorOption ¶ added in v0.9.0
type CollectorOption func(*ErrorCollector)
CollectorOption defines functional options for ErrorCollector configuration
func WithContext ¶ added in v0.9.0
func WithContext(ctx context.Context) CollectorOption
WithContext sets the context for the collector
func WithMaxErrors ¶ added in v0.9.0
func WithMaxErrors(max int) CollectorOption
WithMaxErrors sets the maximum number of errors to collect
func WithStrictMode ¶ added in v0.9.0
func WithStrictMode(strict bool) CollectorOption
WithStrictMode enables strict mode where the collector returns false when maximum errors are reached (instead of silently dropping errors)
type Error ¶
type Error struct {
Category Category `json:"category"`
Code int `json:"code,omitempty"`
TextCode string `json:"text_code,omitempty"`
Message string `json:"message"`
Source error `json:"-"`
ValidationErrors ValidationErrors `json:"validation_errors,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
RequestID string `json:"request_id,omitempty"`
Timestamp time.Time `json:"timestamp"`
StackTrace StackTrace `json:"stack_trace,omitempty"`
Location *ErrorLocation `json:"location,omitempty"`
Severity Severity `json:"severity"`
}
func FromOzzoValidation ¶ added in v0.4.0
func MapAuthErrors ¶ added in v0.3.0
func MapHTTPErrors ¶ added in v0.3.0
func MapToError ¶ added in v0.2.0
func MapToError(err error, mappers []ErrorMapper) *Error
MapToError converts any error to our Error type using provided mappers
func NewCritical ¶ added in v0.9.0
NewCritical creates a new Error with Critical severity
func NewValidation ¶
func NewValidation(message string, fieldErrors ...FieldError) *Error
func NewValidationFromMap ¶
func NewWarning ¶ added in v0.9.0
NewWarning creates a new Error with Warning severity
func NewWithLocation ¶ added in v0.9.0
func NewWithLocation(message string, category Category, location *ErrorLocation) *Error
NewWithLocation creates a new Error with explicit location setting
func ValidateWithOzzo ¶ added in v0.4.0
func (*Error) AllValidationErrors ¶ added in v0.8.0
func (e *Error) AllValidationErrors() ValidationErrors
func (*Error) ErrorWithStack ¶
func (*Error) GetLocation ¶ added in v0.9.0
func (e *Error) GetLocation() *ErrorLocation
GetLocation returns the location where the error occurred
func (*Error) GetSeverity ¶ added in v0.9.0
GetSeverity returns the severity level of the error
func (*Error) HasLocation ¶ added in v0.9.0
HasLocation returns true if the error has location information
func (*Error) HasSeverity ¶ added in v0.9.0
HasSeverity returns true if the error has the specified severity level
func (*Error) IsAboveSeverity ¶ added in v0.9.0
IsAboveSeverity returns true if the error's severity is above the threshold
func (*Error) MarshalJSON ¶
func (*Error) ToErrorResponse ¶ added in v0.2.0
func (e *Error) ToErrorResponse(includeStack bool, stackTrace StackTrace) ErrorResponse
func (*Error) ValidationMap ¶ added in v0.6.0
ValidationMap returns validation errors as a map for easy template usage
func (*Error) WithLocation ¶ added in v0.9.0
func (e *Error) WithLocation(loc *ErrorLocation) *Error
WithLocation sets the location where the error occurred
func (*Error) WithRequestID ¶
TODO: either remove or rename to WithTraceID
func (*Error) WithSeverity ¶ added in v0.9.0
WithSeverity sets the severity level of the error
func (*Error) WithStackTrace ¶
func (*Error) WithTextCode ¶
type ErrorCollector ¶ added in v0.9.0
type ErrorCollector struct {
// contains filtered or unexported fields
}
ErrorCollector provides thread-safe collection and aggregation of errors for batch operations and complex error handling scenarios
func NewCollector ¶ added in v0.9.0
func NewCollector(opts ...CollectorOption) *ErrorCollector
NewCollector creates a new ErrorCollector with the provided options
func (*ErrorCollector) Add ¶ added in v0.9.0
func (c *ErrorCollector) Add(err error) bool
Add adds an error to the collector in a thread-safe manner Returns true if the error was added, false if the collector is full and operating in strict mode
func (*ErrorCollector) AddFieldErrors ¶ added in v0.9.0
func (c *ErrorCollector) AddFieldErrors(errors ...FieldError)
AddFieldErrors adds multiple field errors as a single validation error
func (*ErrorCollector) AddRetryable ¶ added in v0.9.0
func (c *ErrorCollector) AddRetryable(err error, category Category, message string)
AddRetryable adds a retryable error to the collector
func (*ErrorCollector) AddValidation ¶ added in v0.9.0
func (c *ErrorCollector) AddValidation(field, message string)
AddValidation adds a validation error for a specific field
func (*ErrorCollector) CategoryStats ¶ added in v0.9.0
func (c *ErrorCollector) CategoryStats() map[Category]int
CategoryStats returns the count of errors by category
func (*ErrorCollector) Count ¶ added in v0.9.0
func (c *ErrorCollector) Count() int
Count returns the number of errors currently in the collector
func (*ErrorCollector) Errors ¶ added in v0.9.0
func (c *ErrorCollector) Errors() []*Error
Errors returns a copy of all errors in the collector
func (*ErrorCollector) FilterByCategory ¶ added in v0.9.0
func (c *ErrorCollector) FilterByCategory(cat Category) []*Error
FilterByCategory returns all errors matching the specified category
func (*ErrorCollector) FilterBySeverity ¶ added in v0.9.0
func (c *ErrorCollector) FilterBySeverity(min Severity) []*Error
FilterBySeverity returns all errors with severity at or above the specified minimum
func (*ErrorCollector) GetAllValidationErrors ¶ added in v0.9.0
func (c *ErrorCollector) GetAllValidationErrors() ValidationErrors
GetAllValidationErrors aggregates all validation errors from all collected errors This includes both direct validation errors and validation errors from wrapped errors
func (*ErrorCollector) GetRetryableErrors ¶ added in v0.9.0
func (c *ErrorCollector) GetRetryableErrors() []*Error
GetRetryableErrors returns all errors that could be considered retryable based on their severity level (errors below Critical severity)
func (*ErrorCollector) GetValidationErrors ¶ added in v0.9.0
func (c *ErrorCollector) GetValidationErrors() ValidationErrors
GetValidationErrors aggregates all validation errors from collected errors
func (*ErrorCollector) HasErrors ¶ added in v0.9.0
func (c *ErrorCollector) HasErrors() bool
HasErrors returns true if the collector contains any errors
func (*ErrorCollector) HasRetryableErrors ¶ added in v0.9.0
func (c *ErrorCollector) HasRetryableErrors() bool
HasRetryableErrors returns true if any collected errors are retryable
func (*ErrorCollector) LogErrors ¶ added in v0.9.0
func (c *ErrorCollector) LogErrors(logger *slog.Logger)
LogErrors logs all collected errors using the provided logger Each error is logged individually with its appropriate severity level
func (*ErrorCollector) Merge ¶ added in v0.9.0
func (c *ErrorCollector) Merge() *Error
Merge creates a single aggregate error from all collected errors Returns nil if no errors have been collected
func (*ErrorCollector) MostCommonCategory ¶ added in v0.9.0
func (c *ErrorCollector) MostCommonCategory() Category
MostCommonCategory returns the category with the highest error count
func (*ErrorCollector) Reset ¶ added in v0.9.0
func (c *ErrorCollector) Reset()
Reset clears all errors from the collector
func (*ErrorCollector) SeverityDistribution ¶ added in v0.9.0
func (c *ErrorCollector) SeverityDistribution() map[Severity]int
SeverityDistribution returns the count of errors by severity level
func (*ErrorCollector) ToErrorResponse ¶ added in v0.9.0
func (c *ErrorCollector) ToErrorResponse(includeStack bool) *ErrorResponse
ToErrorResponse converts the collector's state to an HTTP error response If the collector has no errors, returns nil If the collector has exactly one error, returns that error's response If the collector has multiple errors, returns a merged error response
func (*ErrorCollector) ToSlogAttributes ¶ added in v0.9.0
func (c *ErrorCollector) ToSlogAttributes() []slog.Attr
ToSlogAttributes creates slog attributes for the collector's current state Includes error count, context information, and category/severity statistics
type ErrorLocation ¶ added in v0.9.0
type ErrorLocation struct {
File string `json:"file"`
Line int `json:"line"`
Function string `json:"function"`
}
ErrorLocation represents the file, line, and function where an error was created
func Here ¶ added in v0.9.0
func Here() *ErrorLocation
Here captures the location where this function is called This is a convenience function for explicit location capture
func (*ErrorLocation) String ¶ added in v0.9.0
func (loc *ErrorLocation) String() string
String returns a formatted string representation of the location
type ErrorMapper ¶ added in v0.2.0
ErrorMapper is a function that can map specific error types to our Error type
func DefaultErrorMappers ¶ added in v0.3.0
func DefaultErrorMappers() []ErrorMapper
type ErrorResponse ¶ added in v0.2.0
type ErrorResponse struct {
Error *Error `json:"error"`
}
ErrorResponse represents the standard structure for API error responses
type FieldError ¶
type FieldError struct {
Field string `json:"field"`
Message string `json:"message"`
Value any `json:"value,omitempty"`
}
FieldError reprents a single validation error for a given field
func (FieldError) Error ¶
func (e FieldError) Error() string
type LocationConfig ¶ added in v0.9.0
type LocationConfig struct {
// EnableLocationCapture controls whether error locations are captured
EnableLocationCapture bool
}
LocationConfig holds configuration for location capture behavior
type RetryableError ¶ added in v0.5.0
type RetryableError struct {
*BaseError
// contains filtered or unexported fields
}
RetryableError extends Error with retry functionality
func NewNonRetryable ¶ added in v0.5.0
func NewNonRetryable(message string, category Category) *RetryableError
NewNonRetryable creates a non-retryable error (useful for validation, auth, etc.)
func NewRetryable ¶ added in v0.5.0
func NewRetryable(message string, category Category) *RetryableError
func NewRetryableExternal ¶ added in v0.5.0
func NewRetryableExternal(message string) *RetryableError
NewRetryableExternal creates a retryable error for an external service
func NewRetryableOperation ¶ added in v0.5.0
func NewRetryableOperation(message string, delay ...time.Duration) *RetryableError
NewRetryableOperation creates a retryable error for operation failures with a short delay of 500 millis by default
func WrapRetryable ¶ added in v0.5.0
func WrapRetryable(source error, category Category, message string) *RetryableError
func (*RetryableError) Error ¶ added in v0.5.1
func (r *RetryableError) Error() string
func (*RetryableError) IsRetryable ¶ added in v0.5.0
func (r *RetryableError) IsRetryable() bool
IsRetryable returns whether this error should trigger a retry Critical and Fatal errors are never retryable regardless of the retryable flag
func (*RetryableError) RetryDealy ¶ added in v0.5.0
func (r *RetryableError) RetryDealy(attempt int) time.Duration
RetryDelay calculates the delay before the next retry attempt Uses exponential backoff: baseDelay * (2^(attempt-1))
func (*RetryableError) WithCode ¶ added in v0.5.0
func (r *RetryableError) WithCode(code int) *RetryableError
func (*RetryableError) WithLocation ¶ added in v0.9.0
func (r *RetryableError) WithLocation(loc *ErrorLocation) *RetryableError
WithLocation sets the location where the error occurred
func (*RetryableError) WithMetadata ¶ added in v0.5.0
func (r *RetryableError) WithMetadata(metas ...map[string]any) *RetryableError
func (*RetryableError) WithRetryDelay ¶ added in v0.5.0
func (r *RetryableError) WithRetryDelay(delay time.Duration) *RetryableError
WithRetryDelay sets the base delay for retry attempts
func (*RetryableError) WithRetryable ¶ added in v0.5.0
func (r *RetryableError) WithRetryable(retryable bool) *RetryableError
WithRetryable sets whether this error should be retryable
func (*RetryableError) WithSeverity ¶ added in v0.9.0
func (r *RetryableError) WithSeverity(s Severity) *RetryableError
WithSeverity sets the severity level of the retryable error
func (*RetryableError) WithStackTrace ¶ added in v0.5.0
func (r *RetryableError) WithStackTrace() *RetryableError
func (*RetryableError) WithTextCode ¶ added in v0.5.0
func (r *RetryableError) WithTextCode(code string) *RetryableError
type Severity ¶ added in v0.9.0
type Severity int
Severity represents the severity level of an error
func (Severity) MarshalJSON ¶ added in v0.9.0
MarshalJSON implements JSON marshaling for Severity
func (Severity) String ¶ added in v0.9.0
String returns the string representation of the severity level
func (*Severity) UnmarshalJSON ¶ added in v0.9.0
UnmarshalJSON implements JSON unmarshaling for Severity
type StackFrame ¶
type StackFrame struct {
Function string `json:"function"`
File string `json:"file"`
Line int `json:"line"`
}
StackFrame is a single frame in the stack trace
type StackTrace ¶
type StackTrace []StackFrame
StackTrace is a collection of stack frames
func CaptureStackTrace ¶
func CaptureStackTrace(skip int) StackTrace
func (StackTrace) String ¶
func (s StackTrace) String() string
type ValidationErrors ¶
type ValidationErrors []FieldError
ValidationErrors is a collection of field level validation errors
func GetValidationErrors ¶
func GetValidationErrors(err error) (ValidationErrors, bool)
func (ValidationErrors) Error ¶
func (e ValidationErrors) Error() string