errlist

package
v0.0.0-...-aed1ee4 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package errlist implents an error type that contains a list of other errors. The provided Add function correctly handles nil so it may be unconditionally called with an error, even if the error is nil.

Package errlist exports two types, List and Error. List is always used to construct a list of errors, and Error is used when introspecting an error. List does not implement the error interface to prevent an empty list from being returned.

EXAMPLE

func checkStrings(source []string) error {
	var errs errlist.List
	for _, s := range source {
		errs.Add(check(s))
	}
	return errs.Err()
}

func check(s string) error {
	if len(s) < 1 || len(s) > 5 {
		return fmt.Errorf("bad string: %q", s)
	}
	return nil
}

func errHasPrefix(err error, prefix string) bool {
	switch errs := err.(type) {
	case errlist.Error:
		for _, err := range errs.Errors() {
			if errHasPrefix(err, prefix) {
				return true
			}
		}
	default:
		return strings.HasPrefix(err.Error(), prefix)
	}
	return false
}

Index

Constants

This section is empty.

Variables

View Source
var Separator = ", "

Separator is used to separate error messages when calling Error on a list. Only package main should set Separator. It should only be set in an init function defined in the main package.

Functions

This section is empty.

Types

type Error

type Error struct {
	List
}

An Error is a list of errors and implements the error interface. An Error should never be declared directly, use a List and then it's Err method to return a proper error.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) Errors

func (e Error) Errors() []error

Errors returns the list of errors in e.

type Errors

type Errors interface {
	// Errors returns the list of errors associated with the recevier.  It
	// returns nil if there are no errors associated with the recevier.
	Errors() []error
}

Errors is implemented by error types that can return lists of errors.

type List

type List struct {
	Separator string
	// contains filtered or unexported fields
}

List is the working representation of an Error, it does not implement the error interface. Use the Add method to add errors to a List.

Separator may optionally be set as the string to separate errors when displayed. If not set, it defaults to the global Separator value.

func (*List) Add

func (e *List) Add(errs ...error) bool

Add adds all non-nil errs to the list of errors in e and returns true if errs contains a non-nil error. If no non-nil errors are passed Add does nothing and returns false. Add will never add a nil error to the List. If err implementes the Errors interface or its underlying type is a slice of errors then e.Add is called on each individual error.

func (List) Err

func (e List) Err() error

Err returns e as an error of type Error if e has errors, or nil.

Jump to

Keyboard shortcuts

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