errchain

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2016 License: BSD-2-Clause Imports: 2 Imported by: 5

README

errchain

Build Status

errchain is a simple library helping you with error handling.

https://godoc.org/github.com/Jille/errchain

Usage example:

func write(data T) (retErr error) {
	fh, err := os.Create("data.gz.new")
	if err != nil {
		return err
	}
	gz := gzip.NewWriter(fh)
	enc := gob.NewEncoder(gz)
	err = enc.Encode(data)
	errchain.Append(&err, gz.Close())
	errchain.Append(&err, fh.Close())
	if err != nil {
		return err
	}
	return os.Rename("data.gz.new", "data.gz")
}

func read() (data T, retErr error) {
	fh, err := os.Open("data.gz")
	if err != nil {
		return nil, err
	}
	defer errchain.Call(&retErr, fh.Close)
	gz, err := gzip.NewReader(fh)
	if err != nil {
		return nil, err
	}
	defer errchain.Call(&retErr, gz.Close)
	dec := gob.NewDecoder(gz)
	if err := dec.Decode(data); err != nil {
		return nil, err
	}
	return c, nil
}

func main() {
	data, err := read()
	if err == nil {
		data++
		err = write(data)
	}
	if err != nil {
		errs := errchain.List(err)
		if len(errs) > 1 {
			fmt.Printf("%d errors:\n", len(errs))
		}
		for _, e := range errs {
			fmt.Println(e)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(err1 *error, errs ...error)

Append changes err1 to be the combination of err1 and all others (nils allowed).

func Call

func Call(err *error, cb func() error)

Call runs cb and chains the error to err. To be used from defers.

func Chain

func Chain(errs ...error) error

Chain takes errors (or nils) and returns them combined if multiple errors are not nil.

func List

func List(err error) []error

List turns an error in a list of errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

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