Documentation
¶
Overview ¶
Package apperrors provides custom error handling for the Tempo CLI.
Error Handling Strategy:
This package standardizes error handling throughout the Tempo codebase with the following guidelines:
Creating New Errors: Use apperrors.Wrap() to create new errors with context, optionally wrapping an underlying cause:
return apperrors.Wrap("failed to read config file", err, filename)
The first argument after the error is treated as the cause. Additional arguments are formatted into the message.
Error Wrapping: Always use apperrors.Wrap() instead of fmt.Errorf() for error wrapping. This ensures consistent error formatting and proper error chain support:
// Good return apperrors.Wrap("failed to process file", err)
// Avoid return fmt.Errorf("failed to process file: %w", err)
Sentinel Errors: Define package-level sentinel errors using fmt.Errorf() for comparison with errors.Is():
var ErrNotFound = fmt.Errorf("resource not found")
Error Attributes: Use WithAttr() to add structured metadata to errors for debugging:
err := apperrors.Wrap("validation failed", nil) err.(*TempoError).WithAttr("field", "email").WithAttr("value", userInput)
Error Chains: The package properly implements Unwrap() to support Go's error chain traversal with errors.Is() and errors.As().
Logging: Use LogErrorChain() for simple error logging or LogErrorChainWithAttrs() for detailed error information including attributes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogErrorChain ¶
func LogErrorChain(err error)
LogErrorChain logs an error chain to the console.
func LogErrorChainWithAttrs ¶
func LogErrorChainWithAttrs(err error)
LogErrorChainWithAttrs logs an error chain with additional attributes in a structured format.
Types ¶
type TempoError ¶
type TempoError struct {
Message string // Context message for the error
Cause error // Underlying cause of the error, if any
Attrs map[string]any // Additional attributes for the error
}
TempoError represents an error with an additional context message, cause, and attributes.
func NewTempoError ¶
func NewTempoError(message string, cause error) *TempoError
NewTempoError creates a new TempoError instance.
func (*TempoError) Error ¶
func (e *TempoError) Error() string
Error returns the context message of the TempoError.
func (*TempoError) ToJSON ¶
func (e *TempoError) ToJSON() ([]byte, error)
func (*TempoError) Unwrap ¶
func (e *TempoError) Unwrap() error
Unwrap returns the underlying cause of the error, if any.
func (*TempoError) WithAttr ¶
func (e *TempoError) WithAttr(key string, value any) *TempoError
WithAttr adds a key-value pair to the attributes and returns the updated error.
type TempoErrorInterface ¶
TempoErrorInterface defines the interface for TempoError with extended capabilities.