Documentation
¶
Overview ¶
Package errors provides error handling utilities for the gitbak application.
This package implements specialized error types and error handling functions to improve error management throughout the application. It focuses on providing rich context for errors while maintaining compatibility with the standard error handling practices.
Features ¶
- Error wrapping with context
- Standardized error formatting
Usage ¶
Basic error wrapping:
if err != nil { return errors.Wrap(err, "failed to open file") }
Creating a new error:
if value < 0 { return errors.New("value must be non-negative") }
Error Wrapping ¶
The package uses standard error wrapping conventions, allowing errors to be unwrapped and inspected using errors.Is and errors.As.
Error Formatting ¶
Errors created with this package provide consistent, formatted error messages that include:
- The error context (what operation was being attempted)
- The underlying error message
- Optional formatting with variable values
Compatibility ¶
The package is fully compatible with the standard library errors package and can be used as a drop-in replacement with additional functionality.
Thread Safety ¶
All types and functions in this package are safe for concurrent use by multiple goroutines.
Index ¶
- Variables
- func As(err error, target any) bool
- func Errorf(format string, args ...interface{}) error
- func Is(err, target error) bool
- func Join(errs ...error) error
- func New(message string) error
- func Wrap(err error, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type ConfigError
- type GitError
- type LockError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotGitRepository indicates the target path is not a git repository ErrNotGitRepository = errors.New("not a git repository") // ErrLockAcquisitionFailure indicates a lock file could not be acquired ErrLockAcquisitionFailure = errors.New("failed to acquire lock") // ErrAlreadyRunning indicates another gitbak instance is running for this repo ErrAlreadyRunning = errors.New("another gitbak instance is already running for this repository") // ErrGitOperationFailed indicates a git command returned an error ErrGitOperationFailed = errors.New("git operation failed") // ErrInvalidConfiguration indicates an invalid or conflicting user configuration ErrInvalidConfiguration = errors.New("invalid configuration") // ErrInvalidFlag indicates an invalid command-line flag was provided ErrInvalidFlag = errors.New("invalid flag") )
Sentinel errors that can be used with errors.Is() for error type checking
Functions ¶
func As ¶
As finds the first error in err's chain that matches target. This is a convenience function that wraps errors.As.
func Errorf ¶
Errorf creates a new formatted error. This is a convenience function that wraps fmt.Errorf.
func Is ¶
Is reports whether target is in err's chain. This is a convenience function that wraps errors.Is.
func Join ¶
Join joins multiple errors into a single error. This is a convenience function that wraps errors.Join.
func New ¶
New creates a new error with the given message. This is a convenience function that wraps errors.New.
Types ¶
type ConfigError ¶
ConfigError represents an error in the application configuration. It includes the parameter name, its value if available, and the underlying error.
func NewConfigError ¶
func NewConfigError(parameter string, value interface{}, err error) *ConfigError
NewConfigError creates a new ConfigError with the given parameters.
Example ¶
err := NewConfigError("interval", -1, fmt.Errorf("must be positive")) fmt.Println(err)
Output: configuration error for interval = -1: must be positive
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
Error implements the error interface with details about the invalid configuration.
func (*ConfigError) Unwrap ¶
func (e *ConfigError) Unwrap() error
Unwrap returns the underlying error for use with errors.Is and errors.As.
type GitError ¶
GitError represents an error that occurred during a Git operation. It captures the command details, underlying error, and command output.
func NewGitError ¶
NewGitError creates a new GitError with the given parameters.
Example ¶
err := NewGitError("clone", []string{"https://github.com/example/repo.git"}, fmt.Errorf("connection failed"), "") fmt.Println(err)
Output: git clone failed: connection failed
type LockError ¶
LockError represents an error that occurred when interacting with file locks. It includes the lock file path, process ID if available, and underlying error.
func NewLockError ¶
NewLockError creates a new LockError with the given parameters.
Example ¶
err := NewLockError("/tmp/gitbak.lock", 1234, fmt.Errorf("permission denied")) fmt.Println(err)
Output: lock error with file /tmp/gitbak.lock (PID: 1234): permission denied