Documentation
¶
Overview ¶
Package errors provides error utilities with collection support for managing multiple errors.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotImplemented is returned when a feature or method has not been implemented yet. ErrNotImplemented = errors.New("not implemented") // ErrWrongType is returned when a type assertion or type conversion fails due to an unexpected type. ErrWrongType = errors.New("wrong type") // ErrHashCollision is returned when two distinct keys produce the same hash value. // This error indicates that the hash function is not suitable for the given key space, // or that the key distribution is causing unexpected collisions. When this error occurs, // consider using a different hash function or implementing a collision resolution strategy. ErrHashCollision = errors.New("hashing collision") // ErrValidation is returned when a value fails validation checks. // This error is used as a sentinel error that wraps the underlying validation failure, // allowing callers to distinguish validation errors from other error types using errors.Is(). // The validate package automatically wraps validation failures with this error. // When this error occurs, the underlying wrapped error will contain specific details // about what validation failed. ErrValidation = errors.New("validation error") // ErrPanicRecovery is returned when a panic has been recovered during error collection. // This error wraps the recovered panic value along with a stack trace to help debug // the source of the panic. Used by the Collect function to safely handle panics. ErrPanicRecovery = errors.New("recovered from panic") )
Functions ¶
func Collect ¶
func Collect(collector func(errs *Collection)) error
Collect provides a safe way to accumulate errors from multiple operations. It creates a Collection, executes the provided collector function, and returns any accumulated errors. If the collector function panics, the panic is recovered and added to the error collection as an ErrPanicRecovery with stack trace. Returns nil if no errors were collected, a single error if only one was collected, or a joined error if multiple errors were collected.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection is a thread-unsafe utility for accumulating multiple errors. It provides methods to add errors, check for errors, and retrieve them as a single combined error. Use this when you need to collect errors from multiple operations and return them together.
func (*Collection) Add ¶
func (c *Collection) Add(err error)
Add appends an error to the collection. Nil errors are automatically ignored.
func (*Collection) Clear ¶
func (c *Collection) Clear()
Clear removes all errors from the collection, resetting it to an empty state.
func (*Collection) GetError ¶
func (c *Collection) GetError() error
GetError returns the collected errors as a single error. Returns nil if the collection is empty, the single error if there's only one, or a joined error (using errors.Join) if there are multiple errors.
func (*Collection) HasError ¶
func (c *Collection) HasError() bool
HasError returns true if the collection contains at least one error.