Code Monkey home page Code Monkey logo

errs's Introduction

Chainable Go Errors

This package provides two useful error types:

  • Const: to represent constant type errors
  • Error: to represent error chains or lists of errors

Constant errors

The Const error is simply a string that supports the Error interface. This allows us to write constant-time errors. These types of errors are useful when we have very shallow error handling. For example a single struct and its methods can probably make do internally with just constant errors. Constant errors fall apart when errors are surfaced from deep in the call stack from code we do not control. For these cases typically the error originated from a 3rd partly library or even the Go standard library. These errors are often custom in implementation so it becomes hard to distinguish them.

Chainable errors

The Error type is a pair or errors. This allows us to build an arbitrarily long chain of errors that is not possible with fmt.Errorf's '%w' wrapping which only allows a single wrapped error. This solves the problem identified above where we want to wrap a 3rd party error with our own (typically constant error) so it is easier to identify at other layers in our application without losing the type information from the source error.

The Error type should be used with care because it enables lazily wrapping errors to avoid proper fine-grained error handling. For example if we wrap the source error several times with generic container errors, then the final error handling code will include several possible matching conditions for the errors.Is function. So it is strongly encouraged to use this functionality sparingly at only at the point where the error originated.

This does not mean you should not have multiple chained errors however. For example if you have a design in mind with classes of errors, then this is a possible use-case that can be a good tradeoff.

errs's People

Contributors

rhomel avatar

Stargazers

 avatar

Watchers

 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.