warning

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 2 Imported by: 1

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

func Is(err error) bool

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

func Wrap(errs ...error) error

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.

func Wrapf

func Wrapf(err error, f string, x ...any) error

Wrapf wraps a single error in a warning, with a message created using a format string.

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

func As(err error) *Warning

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

func New(msg string, errs ...error) *Warning

New creates a new warning that wraps one or more errors. Note that msg is *not* a format string.

func Newf

func Newf(f string, x ...any) *Warning

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

func (w *Warning) Append(errs ...error) *Warning

Append appends errs as child errors of this warning.

func (*Warning) Error

func (w *Warning) Error() string

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.

func (*Warning) Unwrap

func (w *Warning) Unwrap() []error

Unwrap returns all errors directly wrapped by this warning.

func (*Warning) Wrapf

func (w *Warning) Wrapf(f string, x ...any) *Warning

Wrapf wraps this warning in a new warning with a message using a format. If w doesn't already have a message, it sets the message and returns w.

Jump to

Keyboard shortcuts

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