multierror

package
v0.0.0-...-c7c381b Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2015 License: MIT, Apache-2.0 Imports: 2 Imported by: 0

README

multierror

multierror is a simple Go package for combining multiple errors. This is handy if you are concurrently running operations within a function that returns only a single error.

API

multierror exposes two types.

multierror.Errors is a []error with a receiver method Err(), which returns a multierror.MultiError instance or nil. You use this type to collect your errors by appending to it.

multierror.MultiError implements the error interface. Its Errors field contains the multierror.Errors you originally constructed.

Example

package main

import (
	"fmt"
	"github.com/joeshaw/multierror"
)

func main() {
	// Collect multiple errors together in multierror.Errors
	var e1 multierror.Errors
	e1 = append(e1, fmt.Errorf("Error 1"))
	e1 = append(e1, fmt.Errorf("Error 2"))

	// Get a multierror.MultiError from it
	err := e1.Err()

	// Output: "2 errors: Error 1; Error 2"
	fmt.Println(err)

	// Iterate over the individual errors
	merr := err.(*multierror.MultiError)
	for _, err := range merr.Errors {
		fmt.Println(err) // Output: "Error 1" and "Error 2"
	}

	// If multierror.Errors contains no errors, its Err() returns nil
	var e2 multierror.Errors
	err = e2.Err()

	// Output: "<nil>"
	fmt.Println(err)
}

Documentation

Overview

multierror is a simple Go package for combining multiple errors together.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors []error

The Errors type wraps a slice of errors

func (Errors) Err

func (e Errors) Err() error

Returns a MultiError struct containing this Errors instance, or nil if there are zero errors contained.

type MultiError

type MultiError struct {
	Errors Errors
}

The MultiError type implements the error interface, and contains the Errors used to construct it.

func (*MultiError) Error

func (m *MultiError) Error() string

Returns a concatenated string of the contained errors

Jump to

Keyboard shortcuts

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