errors

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound indicates a resource was not found
	ErrNotFound = errors.New("not found")
	// ErrAlreadyExists indicates a resource already exists
	ErrAlreadyExists = errors.New("already exists")
	// ErrNotLeader indicates the node is not the leader
	ErrNotLeader = errors.New("not leader")
	// ErrNoLeader indicates there is no leader elected
	ErrNoLeader = errors.New("no leader")
	// ErrTimeout indicates an operation timed out
	ErrTimeout = errors.New("timeout")
	// ErrTemporaryFailure indicates a temporary failure
	ErrTemporaryFailure = errors.New("temporary failure")
	// ErrInvalidState indicates an invalid state
	ErrInvalidState = errors.New("invalid state")
	// ErrCanceled indicates the operation was canceled
	ErrCanceled = errors.New("canceled")
	// ErrShutdown indicates the component is shutting down
	ErrShutdown = errors.New("shutdown")
)

Common error types

Functions

func IsFatal

func IsFatal(err error) bool

IsFatal checks if an error is fatal

func IsInvalidInput

func IsInvalidInput(err error) bool

IsInvalidInput checks if an error is due to invalid input

func IsRetriable

func IsRetriable(err error) bool

IsRetriable checks if an error is retriable

func IsTransient

func IsTransient(err error) bool

IsTransient checks if an error is transient

func Join

func Join(errs ...error) error

Join combines multiple errors

func Wrap

func Wrap(op string, err error) error

Wrap wraps an error with operation context

Example
baseErr := errors.New("connection refused")
wrapped := Wrap("database.connect", baseErr)

if IsRetriable(wrapped) {
	fmt.Println("Can retry")
}

// Error string contains operation context
errStr := wrapped.Error()
_ = strings.Contains(errStr, "database.connect")

func Wrapf

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

Wrapf wraps an error with a formatted message

Types

type Category

type Category int

Category represents the category of an error

const (
	// CategoryRetriable indicates the operation can be retried immediately
	CategoryRetriable Category = iota
	// CategoryTransient indicates a temporary failure, retry after delay
	CategoryTransient
	// CategoryFatal indicates an unrecoverable error
	CategoryFatal
	// CategoryInvalidInput indicates a client error (bad request)
	CategoryInvalidInput
	// CategoryUnknown indicates the category is unknown
	CategoryUnknown
)

func GetCategory

func GetCategory(err error) Category

GetCategory returns the category of an error

func (Category) String

func (c Category) String() string

String returns the string representation of the category

type Error

type Error struct {
	// Category is the error category
	Category Category
	// Op is the operation that failed
	Op string
	// Err is the underlying error
	Err error
	// Message is a human-readable message
	Message string
	// Metadata contains additional context
	Metadata map[string]interface{}
	// Timestamp is when the error occurred
	Timestamp time.Time
	// Code is an optional error code
	Code string
}

Error represents a structured error with context

func Fatal

func Fatal(op string, err error) *Error

Fatal creates a fatal error

func InvalidInput

func InvalidInput(op string, err error) *Error

InvalidInput creates an invalid input error

func New

func New(category Category, op string, err error) *Error

New creates a new Error

func Retriable

func Retriable(op string, err error) *Error

Retriable creates a retriable error

Example
err := Retriable("connect.database", ErrTimeout).
	WithMessage("failed to connect to database").
	WithMetadata("host", "db.example.com").
	WithMetadata("timeout", "5s")

fmt.Println("Category:", err.Category)
fmt.Println("Retriable:", IsRetriable(err))
Output:

Category: retriable
Retriable: true

func Transient

func Transient(op string, err error) *Error

Transient creates a transient error

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Is

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

Is implements error matching

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error

func (*Error) WithCode

func (e *Error) WithCode(code string) *Error

WithCode adds an error code

func (*Error) WithMessage

func (e *Error) WithMessage(msg string) *Error

WithMessage adds a message to the error

func (*Error) WithMetadata

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

WithMetadata adds metadata to the error

type MultiError

type MultiError struct {
	Errors []error
	Op     string
}

MultiError represents multiple errors

Example
multi := NewMultiError("batch.process")

for i := 0; i < 3; i++ {
	// Simulate processing
	if i == 1 {
		multi.Add(fmt.Errorf("item %d failed", i))
	}
}

if err := multi.ErrorOrNil(); err != nil {
	fmt.Println("Batch processing had errors")
}
Output:

Batch processing had errors

func NewMultiError

func NewMultiError(op string) *MultiError

NewMultiError creates a new MultiError

func (*MultiError) Add

func (m *MultiError) Add(err error)

Add adds an error to the multi-error

func (*MultiError) Error

func (m *MultiError) Error() string

Error implements the error interface

func (*MultiError) ErrorOrNil

func (m *MultiError) ErrorOrNil() error

ErrorOrNil returns nil if there are no errors

func (*MultiError) Unwrap

func (m *MultiError) Unwrap() error

Unwrap returns the first error

Jump to

Keyboard shortcuts

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