erc

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: Apache-2.0 Imports: 6 Imported by: 51

Documentation

Overview

Package erc provides a simple/fast error aggregation tool for collecting and aggregating errors. The tools are compatible with go's native error wrapping, and are generally safe for use from multiple goroutines, so can simplify error collection patterns in worker-pool patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCtx

func CheckCtx(ctx context.Context, ec *Collector, fn func(context.Context) error)

CheckCtx executes a simple function that takes a context and if it returns an error, adds it to the collector, primarily for use in defer statements.

func Recover

func Recover(ec *Collector)

Recover calls the builtin recover() function and converts it to an error that is populated in the collector.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector is a simplified version of the error collector in github.com/tychoish/emt. The collector is thread safe and aggregates errors which can be resolved as a single error. The constituent errors (and flattened, in the case of wrapped errors), are an *erc.Stack object, which can be introspected as needed.

func (*Collector) Add

func (ec *Collector) Add(err error)

Add collects an error if that error is non-nil.

func (*Collector) Check

func (ec *Collector) Check(fn func() error)

Check executes a simple function and if it returns an error, adds it to the collector, primarily for use in defer statements.

func (*Collector) HasErrors

func (ec *Collector) HasErrors() bool

HasErrors returns true if there are any underlying errors, and false otherwise.

func (*Collector) Iterator

func (ec *Collector) Iterator() fun.Iterator[error]

Iterator produces an iterator for all errors present in the collector. The iterator proceeds from the current error to the oldest error, and will not observe new errors added to the collector. While the iterator isn't safe for current access from multiple go routines, the sequence of errors stored are not modified after creation.

func (*Collector) Recover

func (ec *Collector) Recover(callback func())

RecoverAction calls the builtin recover() function and converts it to an error in the collector, for direct use in defers. The callback function is only called if the panic is non-nil.

func (*Collector) Resolve

func (ec *Collector) Resolve() error

Resolve returns an error of type AggregatedError, or nil if there have been no errors added.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack represents the error type returned by an ErrorCollector when it has more than one error. The implementation provides support for errors.Unwrap and errors.Is, and provides an Errors() method which returns a slice of the constituent errors for additional use.

func (*Stack) Error

func (e *Stack) Error() string

Error produces the aggregated error strings from this method. If the error at the current layer is nil.

func (*Stack) Is

func (e *Stack) Is(err error) bool

Is calls errors.Is on the underlying error to provied compatibility with errors.Is, which takes advantage of this interface.

func (*Stack) Iterator

func (e *Stack) Iterator() fun.Iterator[error]

Iterator returns an iterator object over the contents of the stack. While the content of the iterator is immutable and will not observe new errors added to the stack, the iterator itself is not safe for concurrent access, and must only be used from one goroutine, or with a synchronized approach. You may create multiple Iterators on the same stack without issue.

func (*Stack) Unwrap

func (e *Stack) Unwrap() error

Unwrap returns the next iterator in the stack, and is compatible with errors.Unwrap.

Jump to

Keyboard shortcuts

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