Documentation
¶
Overview ¶
Package errext contains convenience functions for handling and propagating errors.
Index ¶
- func As[T error](err error) (T, bool)
- func IsOfType[T error](err error) bool
- type ErrorSet
- func (errs *ErrorSet) Add(err error)
- func (errs *ErrorSet) Addf(msg string, args ...any)
- func (errs *ErrorSet) Append(other ErrorSet)
- func (errs ErrorSet) IsEmpty() bool
- func (errs ErrorSet) Join(sep string) string
- func (errs ErrorSet) JoinedError(sep string) error
- func (errs ErrorSet) LogFatalIfError()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As is a variant of errors.As() that leverages generics to present a nicer interface.
//this code:
var perr os.PathError
if errors.As(err, &perr) {
handle(perr)
}
//can be rewritten as:
if perr, ok := errext.As[os.PathError](err); ok {
handle(perr)
}
This is sometimes more verbose (like in this example), but allows to scope the specific error variable to the condition's then-branch, and also looks more idiomatic to developers already familiar with type casts.
Types ¶
type ErrorSet ¶
type ErrorSet []error
ErrorSet replaces the "error" return value in functions that can return multiple errors. It provides convenience functions for easily adding errors to the set.
func (ErrorSet) Join ¶
Join joins the messages of all errors in this set using the provided separator. If the set is empty, an empty string is returned.
func (ErrorSet) JoinedError ¶
JoinedError is like Join, but returns an error that can be unwrapped.
func (ErrorSet) LogFatalIfError ¶
func (errs ErrorSet) LogFatalIfError()
LogFatalIfError reports all errors in this set on level FATAL, thus dying if there are any errors.