Documentation
¶
Index ¶
- func Aggregate(errs ...error) error
- func All[T any](err error) (results []T)
- func As(err error, target interface{}) bool
- func Check[T ~bool](err error) bool
- func Enrich(err error, enrichments ...Enricher) error
- func Errorf(msg string, args ...any) error
- func Get[T any](err error, def T) T
- func Is(err error, target error) bool
- func New(msg string) error
- func Visit(err error, fn func(error) bool)
- type Enricher
- type ErrorCollector
- type ErrorList
- type TypedErrorCollector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Aggregate ¶
Aggregate returns an error wrapping multiple other errors. If no other errors are passed in this method returns nil.
func All ¶
All returns the enriched context if it exists in the error. As opposed to Get this function returns all the enriched values instead of stopping at the first one.
func As ¶
As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
func Check ¶
Check returns true if the error contains the specified context and it is true.
Example ¶
// Temporary indicates the error is Temporary
type Temporary bool
// Call function returning error
err := functionReturningError()
if err != nil {
// If the error is Temporary then do nothing
if errors.Check[Temporary](err) {
return
}
// Print the error message
fmt.Println(err)
}
func Get ¶
Get returns the enriched context if it exists in the error, otherwise it returns the provided default value.
Example ¶
// UserFacingMessage is a type containing a user facing message
type UserFacingMessage string
// Call function returning error
err := functionReturningError()
if err != nil {
// Get the user facing message
msg := errors.Get[UserFacingMessage](err, "Internal server error")
// Print the error message
fmt.Println(msg)
}
Output: Internal server error
Types ¶
type Enricher ¶
Enricher is an error enrichment, it adds additional context to the provided error.
func Set ¶
Set enriches an error with a specific type.
Example ¶
// UserFacingMessage is a type containing a user facing message
type UserFacingMessage string
// Enrich error using Set method
err := errors.Enrich(functionReturningError(), errors.Set[UserFacingMessage]("Well this sucks."))
// Print the error
fmt.Println(errors.Get[UserFacingMessage](err, "Internal server error"))
Output: Well this sucks.
func WithStackN ¶
WithStackN will enrich the message with a stacktrace, skipping a specified number of calls.
type ErrorCollector ¶
type ErrorCollector struct {
ErrorList
}
ErrorCollector is a wrapper for ErrorList that provides methods for adding errors if they are not nil
func NewErrorCollector ¶
func NewErrorCollector() *ErrorCollector
func (*ErrorCollector) AppendErrorIfNotNil ¶
func (e *ErrorCollector) AppendErrorIfNotNil(err error)
AppendErrorIfNotNil will append the error to the ErrorList if it is not nil, it is designed to allow for a pattern where function call is passed right into the method, for example:
collection.AppendErrorIfNotNil(someFunctionThatMayError())
type ErrorList ¶
type ErrorList []error
ErrorList is a list of errors, it can be used as a utility to aggregate errors by appending to the slice
type TypedErrorCollector ¶
type TypedErrorCollector[T any] struct { // contains filtered or unexported fields }
TypedErrorCollector that allows you to pass through return values while keeping error
func ErrorCollectorForType ¶
func ErrorCollectorForType[T any](collector *ErrorCollector) TypedErrorCollector[T]
ErrorCollectorForType returns a TypedErrorCollector that will collect errors onto a given ErrorCollector
func (TypedErrorCollector[T]) AppendErrorIfNotNil ¶
func (e TypedErrorCollector[T]) AppendErrorIfNotNil(result T, err error) T
AppendErrorIfNotNil will append the error to the ErrorList if it is not nil and return the specified value. It is designed to allow for a pattern where function call is passed right into the method, for example:
value := collection.AppendErrorIfNotNil(someFunctionThatMayError())