ewrap

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package ewrap provides enhanced error handling capabilities with stack traces, error wrapping, custom error types, and logging integration.

Package ewrap provides enhanced error handling capabilities

Package ewrap provides enhanced error handling capabilities

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureStack

func CaptureStack() []uintptr

CaptureStack captures the current stack trace.

Types

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker implements the circuit breaker pattern for error handling.

func NewCircuitBreaker

func NewCircuitBreaker(name string, maxFailures int, timeout time.Duration) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker.

func (*CircuitBreaker) CanExecute

func (cb *CircuitBreaker) CanExecute() bool

CanExecute checks if the operation can be executed.

func (*CircuitBreaker) OnStateChange

func (cb *CircuitBreaker) OnStateChange(callback func(name string, from, to CircuitState))

OnStateChange sets a callback for state changes.

func (*CircuitBreaker) RecordFailure

func (cb *CircuitBreaker) RecordFailure()

RecordFailure records a failure and potentially opens the circuit.

func (*CircuitBreaker) RecordSuccess

func (cb *CircuitBreaker) RecordSuccess()

RecordSuccess records a success and potentially closes the circuit.

type CircuitState

type CircuitState int

CircuitState represents the state of a circuit breaker.

const (
	// CircuitClosed indicates normal operation.
	CircuitClosed CircuitState = iota
	// CircuitOpen indicates the circuit is broken.
	CircuitOpen
	// CircuitHalfOpen indicates the circuit is testing recovery.
	CircuitHalfOpen
)

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents a custom error type with stack trace and metadata.

Example (WithLogger)
logger := newTestLogger()
err := New("database connection failed", WithLogger(logger)).
	WithMetadata("retry_count", 3).
	WithMetadata("last_error", "connection timeout")

err.Log() // This will use the configured logger

// In real usage, you might use your preferred logging framework

func New

func New(msg string, opts ...Option) *Error

New creates a new Error with a stack trace and applies the provided options.

func Wrap

func Wrap(err error, msg string, opts ...Option) *Error

Wrap wraps an existing error with additional context and stack trace.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) *Error

Wrapf wraps an error with a formatted message.

func (*Error) CanRetry

func (e *Error) CanRetry() bool

CanRetry checks if the error can be retried.

func (*Error) Cause

func (e *Error) Cause() error

Cause returns the underlying cause of the error.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) GetMetadata

func (e *Error) GetMetadata(key string) (interface{}, bool)

GetMetadata retrieves metadata from the error.

func (*Error) IncrementRetry

func (e *Error) IncrementRetry()

IncrementRetry increments the retry counter.

func (*Error) Is

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

Is reports whether target matches err in the error chain.

func (*Error) Log

func (e *Error) Log()

Log logs the error using the configured logger.

func (*Error) Stack

func (e *Error) Stack() string

Stack returns the stack trace as a string.

func (*Error) ToJSON

func (e *Error) ToJSON(opts ...FormatOption) (string, error)

ToJSON converts the error to a JSON string.

func (*Error) ToYAML

func (e *Error) ToYAML(opts ...FormatOption) (string, error)

ToYAML converts the error to a YAML string.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap provides compatibility with Go 1.13 error chains.

func (*Error) WithMetadata

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

WithMetadata adds metadata to the error.

Example
err := New("database connection failed").
	WithMetadata("retry_count", 3).
	WithMetadata("last_error", "connection timeout")

fmt.Printf("Error: %v\n", err)

if retryCount, ok := err.GetMetadata("retry_count"); ok {
	
Output:

Error: database connection failed
Retry count: 3

type ErrorContext

type ErrorContext struct {
	// Timestamp when the error occurred.
	Timestamp time.Time
	// Type categorizes the error.
	Type ErrorType
	// Severity indicates the error's impact level.
	Severity Severity
	// Operation that was being performed.
	Operation string
	// Component where the error originated.
	Component string
	// RequestID for tracing.
	RequestID string
	// User associated with the operation.
	User string
	// Environment where the error occurred.
	Environment string
	// Version of the application.
	Version string
	// File and line where the error occurred.
	File string
	Line int
	// Additional context-specific data.
	Data map[string]interface{}
}

ErrorContext holds comprehensive information about an error's context.

func (*ErrorContext) String

func (ec *ErrorContext) String() string

String returns a formatted string representation of the error context.

type ErrorGroup

type ErrorGroup struct {
	// contains filtered or unexported fields
}

ErrorGroup represents a collection of related errors. It maintains a reference to its pool for proper release handling.

func NewErrorGroup

func NewErrorGroup() *ErrorGroup

NewErrorGroup creates a standalone ErrorGroup without pooling. This is useful for cases where pooling isn't needed or for testing.

func (*ErrorGroup) Add

func (eg *ErrorGroup) Add(err error)

Add appends an error to the group if it's not nil.

func (*ErrorGroup) Clear

func (eg *ErrorGroup) Clear()

Clear removes all errors from the group while preserving capacity.

func (*ErrorGroup) Error

func (eg *ErrorGroup) Error() string

Error implements the error interface.

func (*ErrorGroup) Errors

func (eg *ErrorGroup) Errors() []error

Errors returns a copy of all errors in the group.

func (*ErrorGroup) HasErrors

func (eg *ErrorGroup) HasErrors() bool

HasErrors returns true if the group contains any errors.

func (*ErrorGroup) Release

func (eg *ErrorGroup) Release()

Release returns the ErrorGroup to its pool if it came from one. If the ErrorGroup wasn't created from a pool, Release is a no-op.

type ErrorGroupPool

type ErrorGroupPool struct {
	// contains filtered or unexported fields
}

ErrorGroupPool manages a pool of ErrorGroup instances. By making this a separate type, we give users control over pool lifecycle and configuration while maintaining encapsulation.

func NewErrorGroupPool

func NewErrorGroupPool(initialCapacity int) *ErrorGroupPool

NewErrorGroupPool creates a new pool for error groups with the specified initial capacity for the error slices.

func (*ErrorGroupPool) Get

func (p *ErrorGroupPool) Get() *ErrorGroup

Get retrieves an ErrorGroup from the pool or creates a new one if the pool is empty.

type ErrorOutput

type ErrorOutput struct {
	// Message contains the main error message
	Message string `json:"message" yaml:"message"`
	// Timestamp indicates when the error occurred
	Timestamp time.Time `json:"timestamp" yaml:"timestamp"`
	// Type categorizes the error
	Type string `json:"type" yaml:"type"`
	// Severity indicates the error's impact level
	Severity string `json:"severity" yaml:"severity"`
	// Stack contains the error stack trace
	Stack string `json:"stack" yaml:"stack"`
	// Cause contains the underlying error if any
	Cause *ErrorOutput `json:"cause,omitempty" yaml:"cause,omitempty"`
	// Context contains additional error context
	Context map[string]interface{} `json:"context,omitempty" yaml:"context,omitempty"`
	// Metadata contains user-defined metadata
	Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}

ErrorOutput represents a formatted error output structure that can be serialized to various formats like JSON and YAML.

type ErrorType

type ErrorType int

ErrorType represents the type of error that occurred.

const (
	// ErrorTypeUnknown represents an unknown error type.
	ErrorTypeUnknown ErrorType = iota
	// ErrorTypeValidation represents a validation error.
	ErrorTypeValidation
	// ErrorTypeNotFound represents a not found error.
	ErrorTypeNotFound
	// ErrorTypePermission represents a permission error.
	ErrorTypePermission
	// ErrorTypeDatabase represents a database error.
	ErrorTypeDatabase
	// ErrorTypeNetwork represents a network error.
	ErrorTypeNetwork
	// ErrorTypeConfiguration represents a configuration error.
	ErrorTypeConfiguration
	// ErrorTypeInternal indicates internal system errors.
	ErrorTypeInternal
	// ErrorTypeExternal indicates errors from external services.
	ErrorTypeExternal
)

func (ErrorType) String

func (et ErrorType) String() string

String returns the string representation of the error type, useful for logging and error reporting.

type FormatOption

type FormatOption func(*ErrorOutput)

FormatOption defines formatting options for error output.

func WithStackTrace

func WithStackTrace(include bool) FormatOption

WithStackTrace controls whether to include the stack trace in the output.

func WithTimestampFormat

func WithTimestampFormat(format string) FormatOption

WithTimestampFormat allows customizing the timestamp format in the output.

type Option

type Option func(*Error)

Option defines the signature for configuration options.

func WithContext

func WithContext(ctx context.Context, errorType ErrorType, severity Severity) Option

WithContext adds context information to the error.

func WithLogger

func WithLogger(logger logger.Logger) Option

WithLogger sets a logger for the error.

func WithRetry

func WithRetry(maxAttempts int, delay time.Duration) Option

WithRetry adds retry information to the error.

type RecoverySuggestion

type RecoverySuggestion struct {
	// Message provides a human-readable explanation.
	Message string
	// Actions lists specific steps that can be taken.
	Actions []string
	// Documentation links to relevant documentation.
	Documentation string
}

RecoverySuggestion provides guidance on how to recover from an error.

type RetryInfo

type RetryInfo struct {
	// MaxAttempts is the maximum number of retry attempts.
	MaxAttempts int
	// CurrentAttempt is the current retry attempt.
	CurrentAttempt int
	// Delay is the delay between retry attempts.
	Delay time.Duration
	// LastAttempt is the timestamp of the last retry attempt.
	LastAttempt time.Time
	// ShouldRetry is a function that determines if a retry should be attempted.
	ShouldRetry func(error) bool
}

RetryInfo holds information about retry attempts.

type Severity

type Severity int

Severity represents the impact level of an error.

const (
	// SeverityInfo indicates an informational message.
	SeverityInfo Severity = iota
	// SeverityWarning indicates a warning that needs attention.
	SeverityWarning
	// SeverityError indicates a significant error.
	SeverityError
	// SeverityCritical indicates a critical system failure.
	SeverityCritical
)

func (Severity) String

func (s Severity) String() string

String returns the string representation of the severity level.

Directories

Path Synopsis
Package adapters provides logging adapters for popular logging frameworks
Package adapters provides logging adapters for popular logging frameworks

Jump to

Keyboard shortcuts

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