errs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2020 License: MIT Imports: 4 Imported by: 8

Documentation

Overview

Package errs provides error handlers for functions.

The purpose of the errs (short for error handler) package is to provide an error handler base for functions that might encounter errors which they do not know what to do with.

For example, there are many valid ways to respond to an error when processing a directory of files. You might want to ignore them, print them, handle them specially depending on the error.

When writing a function that takes an error handler, allow the user to set a default handler and then pass in nil to specify that the default is desired:

func ACME(h errs.Handler, args ...interface{}) error {
    errs.Init(&h)
    ...
    if err = h(err); err != nil {
        return err
    }
    ...
    return nil
}

Additionally, the MultipleErrors type is provided to wrap multiple errors, in case no error handler is desired.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ignore

func Ignore(_ error) error

Ignore simply ignores all errors. Not recommended.

func Init

func Init(h *Handler)

Init is called by functions that take an Handler to make sure that an Handler is in fact available. It is usually valid to provide functions that expect an Handler a nil, because this function replaces that value with the default Handler, which you can set yourself.

func Quit

func Quit(err error) error

Quit returns whatever error it receives, which causes functions to quit whenever a non-nil error is received. If in doubt, do not use this.

Types

type Collector

type Collector struct {
	Message string
	Errors  []error
}

Collector collects multiple errors and returns a MultipleError if any of the errors are non-nil.

func NewCollector

func NewCollector(msg string) *Collector

func (*Collector) Add

func (c *Collector) Add(err error)

Add adds err to the list of errors, without checking whether it is nil or not.

func (*Collector) Collect

func (c *Collector) Collect(err error)

Collect adds err if it is non-nil.

func (*Collector) Error

func (c *Collector) Error() error

Error returns a MultipleError if it contains any errors, otherwise it returns nil.

type Handler

type Handler func(error) error

Handler is used by many functions to deal with errors, most of which will be nil errors.

There are several Handlers already available for use. Most functions expect that you return nil. Program functionality may be impaired otherwise.

var Default Handler = Print(os.Stderr)

Default is the default Handler that should be used when nil is supplied, see AssertHandler.

func Bundle

func Bundle(el *List) Handler

Bundle bundles all received errors into an ErrorList, which you have to supply (as el).

func Channel

func Channel(ch chan<- error) Handler

Channel sends all received errors that are not nil to the supplied channel.

func Print

func Print(w io.Writer) Handler

Print prints any errors to the writer supplied here. The format it uses is:

error: %s.\n

type List

type List []error

List is a list of errors, and is used by Bundle.

type MultipleError

type MultipleError struct {
	Message string
	Errors  []error
}

func (*MultipleError) Error

func (e *MultipleError) Error() string

Jump to

Keyboard shortcuts

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