errs

package
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Overview

Package errs provides error wrapping functions that allow an error to wrap multiple errors. The error wrapping in the Go standard library errors package allows an error to wrap only one error with one %w format verb in the format string passed to fmt.Errorf and the errors.Unwrap() function that returns a single error.

The error type returned by the functions in this package wraps multiple errors so that errors.Is() and errors.As() can be used to query if that error is any one of the wrapped errors. errors.Unwrap() on errors of that type always returns nil as that function cannot return multiple errors.

The error type is not exported so can only used through the standard Go error interfaces.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf

func Errorf(format string, args ...interface{}) error

Errorf returns an error formatted with a format string and arguments, similar to how fmt.Errorf works. The error returned by Errorf wraps each argument that is an error type unless that error argument is marked with the Nowrap() function. The %w format verb should not be used in the format string.

Example
package main

import (
	"errors"
	"fmt"
	"io"

	"foxygo.at/s/errs"
)

func main() {
	errInternal := errors.New("internal error")
	err := errs.Errorf("%v, caused by: %v", errInternal, io.ErrUnexpectedEOF)

	fmt.Println(err)
	fmt.Println("err is errInternal:", errors.Is(err, errInternal))
	fmt.Println("err is ErrUnexpectedEOF:", errors.Is(err, io.ErrUnexpectedEOF))
}
Output:

internal error, caused by: unexpected EOF
err is errInternal: true
err is ErrUnexpectedEOF: true

func Ignore added in v0.0.35

func Ignore(f func() error)

Ignore executes nullary input function f and ignores its error return value. A typical use case is ignoring errors in the defer statements:

defer errs.Ignore(body.Close)

func New

func New(errs ...error) error

New takes a variable number of input errors and returns an error that is formatted as the concatenation of the string form of each input error, separated by a colon and space. The returned error wraps each input error.

For example New(err1, err2, err3) is formatted as

err1: err2: err3
Example
package main

import (
	"errors"
	"fmt"
	"io"

	"foxygo.at/s/errs"
)

func main() {
	errInternal := errors.New("internal error")
	err := errs.New(errInternal, io.ErrUnexpectedEOF)

	fmt.Println(err)
	fmt.Println("err is errInternal:", errors.Is(err, errInternal))
	fmt.Println("err is ErrUnexpectedEOF:", errors.Is(err, io.ErrUnexpectedEOF))
}
Output:

internal error: unexpected EOF
err is errInternal: true
err is ErrUnexpectedEOF: true

func NoWrap

func NoWrap(err error) noWrap

NoWrap marks errors not to be wrapped for Errorf.

Example
package main

import (
	"errors"
	"fmt"
	"io"

	"foxygo.at/s/errs"
)

func main() {
	errInternal := errors.New("internal error")
	err := errs.Errorf("%v, caused by: %v", errInternal, errs.NoWrap(io.ErrUnexpectedEOF))

	fmt.Println(err)
	fmt.Println("err is errInternal:", errors.Is(err, errInternal))
	fmt.Println("err is ErrUnexpectedEOF:", errors.Is(err, io.ErrUnexpectedEOF))
}
Output:

internal error, caused by: unexpected EOF
err is errInternal: true
err is ErrUnexpectedEOF: false

Types

This section is empty.

Jump to

Keyboard shortcuts

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