errors

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: MIT Imports: 4 Imported by: 0

README

errors

A library with sane error processing primitives

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And(err1 error, err2 error) error

And creates a new combined error. Avoid `err = errors.And(err1, err)` and use `err = errors.And(err, err1)` instead when collecting numerous errors. This will save some memory allocations.

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.And(io.EOF, io.EOF))
	fmt.Println(errors.And(nil, io.EOF))
	fmt.Println(errors.And(io.EOF, nil))

}
Output:

EOF; EOF
EOF
EOF

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	var ce customError
	err := errors.Wrap(customError("message"), "message")
	err = customWrapper{err: err}
	if errors.As(err, &ce) {
		fmt.Println(ce)
	}
	if !errors.As(err, &fakeError{}) {
		fmt.Println(err)
	}
	errs := errors.And(io.EOF, err)
	errs = errors.And(errs, io.EOF)
	if errors.As(errs, &ce) {
		fmt.Println(ce)
	}

	fmt.Println(errors.As(errs, &fakeError{}))

}

type customWrapper struct {
	err error
}

func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }

type customError string

func (ce customError) Error() string { return "error " + string(ce) }

type fakeError struct{}

func (ce fakeError) Error() string { return "fake error" }
Output:

error message
custom wrap: message: error message
error message
false

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	ce := customError("message")
	err := errors.Wrap(customError("message"), "message")
	err = customWrapper{err: err}

	fmt.Println(errors.Is(err, ce))
	fmt.Println(errors.Is(err, fakeError{}))

	errs := errors.And(io.EOF, err)
	errs = errors.And(errs, io.EOF)
	fmt.Println(errors.Is(errs, ce))
	fmt.Println(errors.Is(errs, fakeError{}))

	fmt.Println("checking nil cases")
	fmt.Println(errors.Is(nil, nil))
	fmt.Println(errors.Is(io.EOF, nil))
	fmt.Println(errors.Is(nil, io.EOF))

}

type customWrapper struct {
	err error
}

func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }

type customError string

func (ce customError) Error() string { return "error " + string(ce) }

type fakeError struct{}

func (ce fakeError) Error() string { return "fake error" }
Output:

true
false
true
false
checking nil cases
true
false
false

func New

func New(msg string) error

New just an alias for stdlib's New

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.New("error"))

}
Output:

error

func Newf

func Newf(format string, a ...interface{}) error

Newf an alias for fmt.Errorf

Example
package main

import (
	"fmt"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.Newf("error %s", "message"))

}
Output:

error message

func Wrap

func Wrap(err error, msg string) error

Wrap consturcts a new error by wrapping given message into an error

Example
package main

import (
	"fmt"
	"io"

	"github.com/sirkon/errors"
)

func main() {
	fmt.Println(errors.Wrap(io.EOF, "error"))
	fmt.Println(errors.Wrap(errors.Wrapf(io.EOF, "msg %d", 1), "error"))

}
Output:

error: EOF
error: msg 1: EOF

func Wrapf

func Wrapf(err error, format string, a ...interface{}) error

Wrapf calls Wrap function with a message built with given format

Types

type List

type List []error

List list of errors

func (List) As

func (ce List) As(target interface{}) bool

As applies As function with given target to each error in a list until success

func (List) Error

func (ce List) Error() string

func (List) Is

func (ce List) Is(err error) bool

Is just like As Is applies Is function with given err to each error in a list until success

Jump to

Keyboard shortcuts

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