Documentation
¶
Index ¶
- Variables
- func IsFatal(err error) bool
- func IsInvalidInput(err error) bool
- func IsRetriable(err error) bool
- func IsTransient(err error) bool
- func Join(errs ...error) error
- func Wrap(op string, err error) error
- func Wrapf(op string, err error, format string, args ...interface{}) error
- type Category
- type Error
- type MultiError
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 IsInvalidInput ¶
IsInvalidInput checks if an error is due to invalid input
func Wrap ¶
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")
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 ¶
GetCategory returns the category of an error
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 InvalidInput ¶
InvalidInput creates an invalid input error
func Retriable ¶
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 (*Error) WithMessage ¶
WithMessage adds a message to the error
func (*Error) WithMetadata ¶
WithMetadata adds metadata to the error
type MultiError ¶
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) ErrorOrNil ¶
func (m *MultiError) ErrorOrNil() error
ErrorOrNil returns nil if there are no errors
Click to show internal directories.
Click to hide internal directories.