errext

package
v0.0.0-...-d430ac9 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 6

Documentation

Overview

Package errext contains convenience functions for handling and propagating errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As[T error](err error) (T, bool)

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.

func IsOfType

func IsOfType[T error](err error) bool

IsOfType is a variant of errors.As() that only returns whether the match succeeded.

This function is not called Is() to avoid confusion with errors.Is(), which works differently.

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) Add

func (errs *ErrorSet) Add(err error)

Add adds the given error to the set if it is non-nil.

func (*ErrorSet) Addf

func (errs *ErrorSet) Addf(msg string, args ...any)

Addf is a shorthand for errs.Add(fmt.Errorf(...)).

func (*ErrorSet) Append

func (errs *ErrorSet) Append(other ErrorSet)

Append adds all errors from the `other` ErrorSet to this one.

func (ErrorSet) IsEmpty

func (errs ErrorSet) IsEmpty() bool

IsEmpty returns true if no errors are in the set.

func (ErrorSet) Join

func (errs ErrorSet) Join(sep string) string

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) LogFatalIfError

func (errs ErrorSet) LogFatalIfError()

LogFatalIfError reports all errors in this set on level FATAL, thus dying if there are any errors.

Jump to

Keyboard shortcuts

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