multierror

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2016 License: MIT Imports: 1 Imported by: 6

README

go-multierror

A tiny Go library for bundling together multiple error objects

godoc Build Status Coverage Status GitHub release

import (
	"errors"

	"gopkg.in/multierror.v1"
)

var (
	A = errors.New("A")
	B = errors.New("B")
	C = errors.New("C")
)

func Demo(a, b, c bool) error {
	errors := []error(nil)
	if a {
		errors = append(errors, A)
	}
	if b {
		errors = append(errors, B)
	}
	if c {
		errors = append(errors, C)
	}
	return multierror.New(errors)
}

// Demo(false, false, false) returns nil
// Demo(true,  false, false) returns A
// Demo(true,  true,  true)  returns MultiError{A, B, C}
//
// MultiError{A, B, C} stringifies as:
//	encountered multiple errors:
//		... A
//		... B
//		... C

Documentation

Overview

Package multierror provides a type for composite errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(errors []error) error

New returns a (possibly composite) error that represents the errors in the provided list.

Any nil errors in the list are removed, and any MultipleErrors instances in the list are replaced with their inlined contents (recursively). If the resulting list is empty, error(nil) is returned. If the resulting list contains a single error, that error is returned directly. Otherwise, an instance of MultipleErrors is returned.

func Of

func Of(errors ...error) error

Of is syntactic sugar; it simply calls New with the given errors.

Types

type MultipleErrors

type MultipleErrors []error

MultipleErrors is an implementation of error that is made of other errors. This is useful in situations where it makes sense to collect and present as many errors as possible, e.g. in validating user input, rather than to stop at the first error and report it.

Example (Empty)
fmt.Printf("%T: %[1]v\n", Of())
Output:

<nil>: <nil>
Example (Many)
fmt.Printf("%T: %[1]v\n", Of(errorA, errorB, errorC))
Output:

multierror.MultipleErrors: encountered multiple errors:
	... A
	... B
	... C
Example (Nested)
inner := MultipleErrors{errorA, nil}
mid := MultipleErrors{inner, nil, errorB}
outer := Of(mid, errorC)
fmt.Printf("%T: %[1]v\n", Of(errorA, errorB, errorC))
fmt.Printf("Len=%d\n", len(outer.(MultipleErrors)))
Output:

multierror.MultipleErrors: encountered multiple errors:
	... A
	... B
	... C
Len=3
Example (Nil)
fmt.Printf("%T: %[1]v\n", Of(nil, nil, nil))
Output:

<nil>: <nil>
Example (One)
fmt.Printf("%T: %[1]v\n", Of(errorA))
Output:

multierror.exampleError: A

func (MultipleErrors) Error

func (e MultipleErrors) Error() string

Jump to

Keyboard shortcuts

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