Documentation ¶
Overview ¶
Package warning provides a warning error type (a "soft" multi-error).
Example usage that catches and produces warnings:
func unmarshalThings(src []any) ([]thing, error) { var dst []thing var warns []error for i, a := range src { // Unmarshal can produce warnings or errors var x thing err := Unmarshal(a, &x) if w := warning.As(err); w != nil { warns = append(warns, w.Wrapf("while unmarshaling item %d of %d", i, len(src))) } else if err != nil { return err } dst = append(dst, x) } // Since it only had warnings, return both dst and a wrapper warning. return dst, warning.Wrap(warns...) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶
Is reports if err is a *Warning. nil is not considered to be a warning. This is distinct from errors.Is because an error is a warning only if the top level of the tree is a warning (not if any of the recursively-unwrapped errors are).
func Wrap ¶
Wrap returns a new warning with no message that wraps one or more errors. If passed no errors, it returns nil. If passed a single error that is a warning, it returns that warning. This is a convenient way to downgrade an error to a warning, and also handle cases where no warnings occurred.
Types ¶
type Warning ¶
type Warning struct {
// contains filtered or unexported fields
}
Warning is a kind of error that exists so that parsing/processing functions can produce warnings that do not abort part-way, but can still be reported via the error interface and logged. Warning can wrap zero or more errors (that may also be warnings). A Warning with zero wrapped errors is considered equivalent to a nil warning.
func As ¶
As checks if err is a *Warning, and returns it. If it is nil or not a *Warning, it returns nil. This is distinct from errors.As because an error is a warning only if the top level of the tree is a warning (not if any of the recursively-unwrapped errors are).
func New ¶
New creates a new warning that wraps one or more errors. Note that msg is *not* a format string.
func Newf ¶
Newf creates a new warning that wraps a single error created with fmt.Errorf. (This enables the use of %w for wrapping other errors.)
func (*Warning) Error ¶
Error returns a string that generally looks like:
w.message ↳ w.errs[0].Error() ↳ w.errs[1].Error() ↳ ...
If the warning has no message, it is omitted. If there is no message and also only one child error, that child error's Error() is returned directly. Otherwise, Error prepends indentation to sub-error messages that span multiple lines to make them print nicely.