errors

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2017 License: Apache-2.0 Imports: 4 Imported by: 229

README

go-errors GoDoc Build Status codecov codebeat badge

Yet another errors package, implementing errors handling primitives, allowing error wrapping and error tracing.

Installation

The recommended way to install go-errors:

go get -u gopkg.in/src-d/go-errors.v1

Examples

The Kind type allows create new errors containing the stack trace and also allows identify errors generated by it.

var ErrExample = errors.NewKind("example")

err := ErrExample.New()
if ErrExample.Is(err) {
	fmt.Printf("%+v\n", err)
}

// Example Output:
// example
//
// gopkg.in/src-d/errors%2v0_test.ExampleError_Format
//         /home/mcuadros/workspace/go/src/gopkg.in/src-d/errors.v0/example_test.go:60
// testing.runExample
//         /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
//         /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
//         /usr/lib/go/src/testing/testing.go:744
// main.main
//         github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
//         /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
//         /usr/lib/go/src/runtime/asm_amd64.s:2086
Error with format
var ErrMaxLimitReached = errors.NewKind("max. limit reached: %d")

err := ErrMaxLimitReached.New(42)
if ErrMaxLimitReached.Is(err) {
    fmt.Println(err)
}

// Output: max. limit reached: 42
Error wrapping
var ErrNetworking = errors.NewKind("network error")

err := ErrNetworking.Wrap(io.EOF)
if ErrNetworking.Is(err) {
    fmt.Println(err)
}

// Output: network error: EOF

You can find this examples and many others at the examples file.

License

Apache License 2.0, see LICENSE

Documentation

Overview

Yet another `errors` package, implementing errors handling primitives, allowing error wrapping and error tracing.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(err error, matchers ...Matcher) bool

Any checks if err matches any matchers

Example
var ErrNetworking = errors.NewKind("network error")
var ErrReading = errors.NewKind("reading error")

err := ErrNetworking.New()
if errors.Any(err, ErrReading, ErrNetworking) {
	fmt.Println(err)
}
Output:

network error

func Is

func Is(err error, matchers ...Matcher) bool

Is check if err matches all matchers

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents an error of some Kind, implements the error interface

Example (Printf)
var ErrExample = errors.NewKind("example with stack trace")

err := ErrExample.New()
fmt.Printf("%+v\n", err)

// Example Output:
// example with stack trace
//
// gopkg.in/src-d/errors%2v0_test.ExampleError_Format
//         /home/mcuadros/workspace/go/src/gopkg.in/src-d/errors.v1/example_test.go:60
// testing.runExample
//         /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
//         /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
//         /usr/lib/go/src/testing/testing.go:744
// main.main
//         github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
//         /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
//         /usr/lib/go/src/runtime/asm_amd64.s:2086
Output:

func (*Error) Cause

func (err *Error) Cause() error

Cause returns the underlying cause of the error

func (*Error) Error

func (err *Error) Error() string

func (*Error) Format

func (err *Error) Format(s fmt.State, verb rune)

Format implements fmt.Formatter and can be formatted by the fmt package. The following verbs are supported

%s    print the error. If the error has a Cause it will be
      printed recursively
%v    see %s
%+v   extended format. Each Frame of the error's StackTrace will
      be printed in detail.

func (*Error) StackTrace

func (err *Error) StackTrace() StackTrace

StackTrace returns an stack trace of the error

type Kind

type Kind struct {
	Message string
}

Kind represents the kind of an error, from a Kind you can generate as many Error instances as you want of this Kind

func NewKind

func NewKind(msg string) *Kind

NewKind returns a Kind with the given msg

func (*Kind) Is

func (k *Kind) Is(err error) bool

Is checks if the given error or any of its children are of this Kind

func (*Kind) New

func (k *Kind) New(values ...interface{}) *Error

New returns a new Error, values can be passed to it if the Kind was created using printf format

Example
var ErrExample = errors.NewKind("example")

err := ErrExample.New()
if ErrExample.Is(err) {
	fmt.Println(err)
}
Output:

example
Example (Pattern)
var ErrMaxLimitReached = errors.NewKind("max. limit reached: %d")

err := ErrMaxLimitReached.New(42)
if ErrMaxLimitReached.Is(err) {
	fmt.Println(err)
}
Output:

max. limit reached: 42

func (*Kind) Wrap

func (k *Kind) Wrap(cause error, values ...interface{}) *Error

Wrap creates a new Error of this Kind with the cause error, values can be passed to it if the Kind was created using printf format.

Example
var ErrNetworking = errors.NewKind("network error")

err := ErrNetworking.Wrap(io.EOF)
if ErrNetworking.Is(err) {
	fmt.Println(err)
}
Output:

network error: EOF
Example (Nested)
var ErrNetworking = errors.NewKind("network error")
var ErrReading = errors.NewKind("reading error")

err3 := io.EOF
err2 := ErrReading.Wrap(err3)
err1 := ErrNetworking.Wrap(err2)
if ErrReading.Is(err1) {
	fmt.Println(err1)
}
Output:

network error: reading error: EOF
Example (Pattern)
var ErrFileRead = errors.NewKind("error reading %s")

err := ErrFileRead.Wrap(io.ErrUnexpectedEOF, "/tmp/file")
if ErrFileRead.Is(err) {
	fmt.Println(err)
}
Output:

error reading /tmp/file: unexpected EOF

type Matcher

type Matcher interface {
	// Is returns true if the err matches
	Is(err error) bool
}

Matcher matches a given error

type StackTrace

type StackTrace struct {
	errors.StackTrace
}

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func NewStackTrace

func NewStackTrace(skip int) StackTrace

NewStackTrace returns a new StackTrace, skipping the given number of frames, to avoid including the caller

Jump to

Keyboard shortcuts

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