apperrors

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 7 Imported by: 0

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:

  1. 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.

  2. 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)

  3. Sentinel Errors: Define package-level sentinel errors using fmt.Errorf() for comparison with errors.Is():

    var ErrNotFound = fmt.Errorf("resource not found")

  4. 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)

  5. Error Chains: The package properly implements Unwrap() to support Go's error chain traversal with errors.Is() and errors.As().

  6. 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.

func Wrap

func Wrap(msg string, args ...any) error

Wrap creates a TempoError with a given message, optional cause, and formatting arguments. If the first variadic argument is an error, it is treated as the cause.

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

type TempoErrorInterface interface {
	error
	Attrs() map[string]any
}

TempoErrorInterface defines the interface for TempoError with extended capabilities.

Jump to

Keyboard shortcuts

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