Code Monkey home page Code Monkey logo

go-errors's Introduction

go-errors GoDoc Build Status codecov codebeat badge

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

Installation

The recommended way to install go-errors is:

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

Examples

The Kind type allows you to create new errors containing the stack trace and also check if an error is of a particular kind.

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 these examples and many more in the examples file.

License

Apache License 2.0, see LICENSE

go-errors's People

Contributors

abeaumont avatar alcortesm avatar erizocosmico avatar mcuadros avatar smola avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-errors's Issues

[Proposal] Wrap errors with no kind

Sometimes as we pass the error down the line we might want to annotate the error with more information and we don't want to check its type later. For these cases, Kind is an overkill, because you need to declare a kind and then use Wrap.

A Wrap top-level function could be nice for this use-case.

if err != nil {
    return nil, errors.Wrap(err, "couldn't get value at position %d", position)
}

Instead of:

var errCantGetValueAtPos = errors.NewKind("couldn't get value at position %d")

if err != nil {
    return nil, errCantGetValueAtPos.Wrap(err, position)
}

It enables having ad-hoc error messages just for adding info and reduces the amount of kinds that need to be declared.

Reference:

  • xerrors.Errorf in the new experimental errors package that is probably going into Go 1.13.

[Proposal] No way to check the cause if using a mix of go-errors and standard errors

Let's imagine we are using a mix of this library and standard errors created with errors.New or fmt.Errorf.

Consider the following snippet:

var errStd = stderrors.New("something")

var errKind = errors.NewKind("something: %s")

var myErr = errKind.Wrap(errStd, "foo")

If we want to check if myErr has errStd as its cause, we need to do the following:

 err, ok := myErr.(*errors.Error)
if !ok {
  // handle
}
if err.Cause() == errStd {
}

This is potentially more cumbersome if the original error is deeply nested.
Perhaps we could have a function IsCausedBy(err, cause).

In this case, it would become:

if errors.Is(myErr, errStd) {
  // do something
}

Implementation could look like this:

func Is(err, cause error) bool {
    e, ok := err.(*Error)
    if !ok {
        return false
    }
    
    if e, ok := e.cause.(*Error); ok {
        return Is(e, cause)
    }

    return e.cause == cause
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.