GithubHelp home page GithubHelp logo

go-errors's Introduction

go-errors

Drop-in replacement of the native Go errors package that adds value type sentinel errors and error composition.

Latest Build Codecov Go Report Card


Installation

go get -u github.com/maargenton/go-errors
import "github.com/maargenton/go-errors"

Usage

Package errors is a drop-in replacement for the built-in errors package. It forwards all call to the errors API directly to the built-in errors package. As of Go 1.13, this include:

  • errors.New()
  • errors.Unwrap()
  • errors.Is()
  • errors.As()

Sentinel error

Sentinel errors can be defined as const values of type errors.Sentinel. Such sentinel errors are immutable and proper value type. They can be used directly in foreign packages to test against, but they can also be redefined locally to reduce package dependency and still be tested against.

const ErrMyError errors.Sentinel = "ErrMyError"

if errors.Is(err, ErrMyError) {
    // Specific error ErrMyError can be handled here
}

if errors.Is(err, errors.Sentinel("ErrMyError") {
    // This also detects the same error
}

Error composition

With sentinel errors, instead of wrapping one error into another, you might want to compose two or more errors together with errors.Compose(). Typical use-case would compose a sentinel error with either an underlying error or a more detailed error message.

The error returned by errors.Compose() unwraps into the second error argument, but when using errors.Is() or errors.As(), all error branches are evaluated from left to right.

err = errors.Compose(ErrMyError, underlyingError)

errors.Is(err, ErrMyError)      // => true
errors.Is(err, underlyingError) // => true

Sentinel error composition

Sentinel errors are most useful when combined with an additional error message describing what exactly happened. To help with that, the Sentinel error type defines three methods that return a composite error based on the sentinel:

  • Sentinel.New(string)
  • Sentinel.Wrap(error)
  • Sentinel.Errorf(format, ...)

Usage:

const ErrMyError errors.Sentinel = "ErrMyError"

if somethingFailed {
    return ErrMyError.New("this is what happened")
}

if err := doSomething(); err!= nil {
    return ErrMyError.Wrap(err)
}

if err := doSomething(); err!= nil {
    return ErrMyError.Errorf("something really bad happened: %w", err)
}

go-errors's People

Contributors

maargenton avatar

Watchers

James Cloos avatar  avatar

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.