errors

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MIT Imports: 2 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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

func As(err error, target any) bool

As finds the first error in err's chain that matches target. This is a convenience function that wraps errors.As.

func Errorf

func Errorf(format string, args ...interface{}) error

Errorf creates a new formatted error. This is a convenience function that wraps fmt.Errorf.

func Is

func Is(err, target error) bool

Is reports whether target is in err's chain. This is a convenience function that wraps errors.Is.

func Join

func Join(errs ...error) error

Join joins multiple errors into a single error. This is a convenience function that wraps errors.Join.

func New

func New(message string) error

New creates a new error with the given message. This is a convenience function that wraps errors.New.

func Wrap

func Wrap(err error, message string) error

Wrap wraps an error with a message for better context.

Example
err := fmt.Errorf("original error")

wrapped := Wrap(err, "context information")

fmt.Println(wrapped)
Output:

context information: original error

func Wrapf

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

Wrapf wraps an error with a formatted message for better context.

Types

type ConfigError

type ConfigError struct {
	Parameter string
	Value     interface{}
	Err       error
}

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

type GitError struct {
	Operation string
	Args      []string
	Err       error
	Output    string
}

GitError represents an error that occurred during a Git operation. It captures the command details, underlying error, and command output.

func NewGitError

func NewGitError(operation string, args []string, err error, output string) *GitError

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

func (*GitError) Error

func (e *GitError) Error() string

Error implements the error interface with a detailed, user-friendly error message.

func (*GitError) Unwrap

func (e *GitError) Unwrap() error

Unwrap returns the underlying error for use with errors.Is and errors.As.

type LockError

type LockError struct {
	LockFile string
	PID      int
	Err      error
}

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

func NewLockError(lockFile string, pid int, err error) *LockError

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

func (*LockError) Error

func (e *LockError) Error() string

Error implements the error interface with details about the lock file and process.

func (*LockError) Unwrap

func (e *LockError) Unwrap() error

Unwrap returns the underlying error for use with errors.Is and errors.As.

Jump to

Keyboard shortcuts

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