Giter Site home page Giter Site logo

errors's Introduction

errors

tag Go Version GoDoc Go Lint codecov Report card CodeQL Dependency Review License

nice replacement for pkg/errors

Fork of xerrors with explicit Wrap instead of %w with no stack trace.

go get github.com/peczenyj/errors
errors.Wrap(err, "message")

Why

  • Using Wrap is the most explicit way to wrap errors
  • Wrapping with fmt.Errorf("foo: %w", err) is implicit, redundant and error-prone
  • Parsing "foo: %w" is implicit, redundant and slow
  • The pkg/errors and xerrors are not maintainted
  • The cockroachdb/errors is too big
  • The errors has no Wrap
  • The go-faster/errors demands call errors.DisableTrace or build tag noerrtrace to disable stack traces.

If you don't need stack traces, this is the right tool for you.

The motivation behind this package is: if you are using pkg/errors or go-faster/errors should be easier to swich to this package by just change the import (when you can't switch to the standard errors).

It means, if your code is already using method such as Wrap or Wrapf, they are available.

Same for WithMessage and WithMessagef (however, here it is just an alias to Wrap and Wrapf).

The useful function Into from go-faster/errors is also available.

Features

Feature errors pkg/errors go-faster/errors peczenyj/errors
error constructors (New, Errorf)
error causes (Cause / Unwrap)
type safety (Into)
errors.As(), errors.Is()
support stack traces no, by desing

|

Motivation

If your project wants to minimize external dependencies and does not need stack traces on every error or failure, this is a acceptable alternative.

When migrating from some github.com/<your favorite repository>/errors to the standard lib errors, we can see that our code rely on non-standard functions such as Wrap or Wrapf that demands more changes than just update the import.

However, helper function such as Wrap or Wrapf are useful, since it highlight the error and automatically ignores nil values.

It means we can choose between:

data, err := io.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed") // add context
}
...

And a more simple approach:

data, err := io.ReadAll(r)

return data, errors.Wrap(err, "read failed") // will return nil if err is nil

werrors

You can keep using standard errors and import the github.com/peczenyj/errors/werrors to have access to some extra functions such as Wrap, Wrapf, WithMessage, WithMessagef, Cause and Into.

It is a more minimalistic approach.

go get github.com/peczenyj/errors/werrors
werrors.Wrap(err, "message")

errors's People

Contributors

peczenyj avatar

Stargazers

Marco Amador avatar Mihai Todor avatar Antoine GIRARD avatar  avatar

Watchers

 avatar  avatar

errors's Issues

Add support to go 1.18.x

Still around

impact:

  • verify function Into (generics)
  • function Join (available only in 1.20.x)

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.