Documentation
¶
Overview ¶
Package errm is a library for convinient and easy use of errors in a golang code.
Index ¶
- func Check(err error) bool
- func Contains(err error, target string) bool
- func ContainsErr(err, target error) bool
- func Errorf(msg string, args ...any) error
- func Is(err, target error, targets ...error) bool
- func JoinErrors(errs ...error) error
- func New(msg string, fields ...any) error
- func StackForLogger(err error) []any
- func ToJSON(err error) map[string]any
- func Wrap(err error, msg string, fields ...any) error
- func Wrapf(err error, msg string, args ...any) error
- type List
- func (e *List) Add(err error)
- func (e *List) Clear()
- func (e *List) Empty() bool
- func (e *List) Err() error
- func (e *List) Errorf(format string, args ...any)
- func (e *List) Has(err error, errs ...error) bool
- func (e *List) Len() int
- func (e *List) New(err string, fields ...any)
- func (e *List) NotEmpty() bool
- func (e *List) Wrap(err error, format string, fields ...any)
- func (e *List) Wrapf(err error, format string, args ...any)
- type SafeList
- func (e *SafeList) Add(err error)
- func (e *SafeList) Clear()
- func (e *SafeList) Empty() bool
- func (e *SafeList) Err() error
- func (e *SafeList) Errorf(format string, args ...any)
- func (e *SafeList) Has(err error) bool
- func (e *SafeList) Len() int
- func (e *SafeList) New(err string, fields ...any)
- func (e *SafeList) NotEmpty() bool
- func (e *SafeList) Wrap(err error, format string, fields ...any)
- func (e *SafeList) Wrapf(err error, format string, args ...any)
- type SafeSet
- func (e *SafeSet) Add(err error)
- func (e *SafeSet) Clear()
- func (e *SafeSet) Empty() bool
- func (e *SafeSet) Err() error
- func (e *SafeSet) Errorf(format string, args ...any)
- func (e *SafeSet) Has(err error) bool
- func (e *SafeSet) Len() int
- func (e *SafeSet) New(err string, fields ...any)
- func (e *SafeSet) Wrap(err error, format string, fields ...any)
- func (e *SafeSet) Wrapf(err error, format string, args ...any)
- type Set
- func (e *Set) Add(err error)
- func (e *Set) Clear()
- func (e *Set) Empty() bool
- func (e *Set) Err() error
- func (e *Set) Errorf(format string, args ...any)
- func (e *Set) Has(err error, errs ...error) bool
- func (e *Set) Len() int
- func (e *Set) New(msg string, fields ...any)
- func (e *Set) Wrap(err error, format string, fields ...any)
- func (e *Set) Wrapf(err error, format string, args ...any)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check returns true if the provided error is the one that was created using methods from this package.
func ContainsErr ¶
ContainsErr reports whether any error in err's chain contains target string representation.
func Errorf ¶
Errorf creates a new error with a formatted message and pairs of fields in a field=val format.
func JoinErrors ¶
JoinErrors joins error messages using '; ' as separator (instead of '\n' like errors.Join() does).
a := errm.New("first error")
b := errm.New("second error")
JoinErrors(a, b) // "first error; second error"
func StackForLogger ¶
StackForLogger returns slice ["stack", "[...]"] that can be used as fields for logger if you want to log stack trace.
Types ¶
type List ¶
type List struct {
// contains filtered or unexported fields
}
List object is useful for collecting multiple errors into a single error, in which error messages are separated by a ";". This object is not safe for concurrent/parallel usage.
func NewList ¶
func NewList() *List
NewList returns a new List instance with an empty underlying slice. Working with List will cause allocations, use NewListWithCapacity if you know the number of errors.
func NewListWithCapacity ¶
NewListWithCapacity returns a new List instance with an initialized underlying slice. It may be useful if you know the number of errors and you want to optimize code.
func (*List) Add ¶
Add appends an error to the underlying slice. It is noop if you provide an empty error.
type SafeList ¶
type SafeList struct {
List *List
// contains filtered or unexported fields
}
SafeList object is useful for collecting multiple errors from different goroutines into a single error, in which error messages are separated by a ";". It is safe for concurrent/parallel usage.
func NewSafeList ¶
func NewSafeList() *SafeList
NewSafeList returns a new SafeList instance with an empty underlying slice. Working with SafeList will cause allocations, use NewSafeListWithCapacity if you know the number of errors.
func NewSafeListWithCapacity ¶
NewSafeListWithCapacity returns a new SafeList instance with an initialized underlying slice. It may be useful if you know the number of errors and you want to optimize code.
func (*SafeList) Add ¶
Add appends an error to the underlying slice. It is noop if you provide an empty error. It is safe for concurrent/parallel usage.
func (*SafeList) Clear ¶
func (e *SafeList) Clear()
Clear removes underlying slice of errors. It is safe for concurrent/parallel usage.
func (*SafeList) Empty ¶
Empty return true if the SafeList collector is empty. It is safe for concurrent/parallel usage.
func (*SafeList) Err ¶
Err returns current SafeList instance as error interface or nil if it is empty. It is safe for concurrent/parallel usage.
func (*SafeList) Errorf ¶
Errorf creates an error using Errorf and appends in to the underlying slice. It is safe for concurrent/parallel usage.
func (*SafeList) Has ¶
Has returns true if the SafeList contains the given error. It is safe for concurrent/parallel usage.
func (*SafeList) Len ¶
Len returns the number of errors in SafeList. It is safe for concurrent/parallel usage.
func (*SafeList) New ¶
New creates an error using New and appends in to the underlying slice. It is safe for concurrent/parallel usage.
func (*SafeList) NotEmpty ¶
NotEmpty return true if the SafeList collector has errors. It is safe for concurrent/parallel usage.
type SafeSet ¶
type SafeSet struct {
// contains filtered or unexported fields
}
SafeSet object is useful for collecting multiple unique errors from different goroutines into a single error, in which error messages are separated by a ";". It is safe for concurrent/parallel usage.
func NewSafeSet ¶
func NewSafeSet() *SafeSet
NewSafeSet returns a new SafeSet instance with an empty underlying slice. Working with SafeSet will cause allocations, use NewSafeSetWithCapacity if you know the number of unique errors.
func NewSafeSetWithCapacity ¶
NewSafeSetWithCapacity returns a new SafeSet instance with an initialized underlying slice. It may be useful if you know the number of errors and you want to optimize code.
func (*SafeSet) Add ¶
Add sets an error to the underlying map. It is safe for concurrent/parallel usage.
func (*SafeSet) Clear ¶
func (e *SafeSet) Clear()
Clear removes underlying map of errors. It is safe for concurrent/parallel usage.
func (*SafeSet) Empty ¶
Empty return true if the SafeSet collector is empty. It is safe for concurrent/parallel usage.
func (*SafeSet) Err ¶
Err returns current SafeSet instance as error interface or nil if it is empty. It is safe for concurrent/parallel usage.
func (*SafeSet) Errorf ¶
Errorf creates an error using Errorf and sets in to the underlying map. It is safe for concurrent/parallel usage.
func (*SafeSet) Has ¶
Has returns true if the SafeSet contains the given error. It is safe for concurrent/parallel usage.
func (*SafeSet) Len ¶
Len returns the number of errors in SafeSet. It is safe for concurrent/parallel usage.
func (*SafeSet) New ¶
New creates an error using New and sets it to the underlying map. It is safe for concurrent/parallel usage.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set object is useful for collecting multiple unique errors into a single error, in which error messages are separated by a ";". This object is not safe for concurrent/parallel usage. It is not very optimal thing, because it is calling err.Error() to make a key for the map. So you have time-overhead caused by Error() and space-overhead because it stores an error twice (string key and value). But you can win with it versus List when you have a lot of similar errors.
func NewSet ¶
func NewSet() *Set
NewSet returns a new Set instance with an empty underlying map. Working with Set will cause allocations, use NewSetWithCapacity if you know the number of unique errors.
func NewSetWithCapacity ¶
NewSetWithCapacity returns a new Set instance with an initialized underlying map. It may be useful if you know the number of errors and you want to optimize code.
func (*Set) Add ¶
Add sets an error to the underlying map. It is noop if you provide a nil error. It will call err.Error() to make a key for the map.
func (*Set) Errorf ¶
Errorf creates an error using Errorf and sets in to the underlying map. It will call err.Error() to make a key for the map.
func (*Set) New ¶
New creates an error using New and sets in to the underlying map. It will call err.Error() to make a key for the map.