Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add information about github.com/pkg/errors #201

Open
alex-semenyuk opened this issue Nov 18, 2023 · 2 comments
Open

Add information about github.com/pkg/errors #201

alex-semenyuk opened this issue Nov 18, 2023 · 2 comments

Comments

@alex-semenyuk
Copy link
Contributor

At section with Errors Types mentioned only about standart go library errors, but github.com/pkg/errors is popular as well, we can find a lot of references of it at Uber open source repos:
https://github.com/search?q=org%3Auber%20github.com%2Fpkg%2Ferrors&type=code

So propose to add section about it.

The key points in this:

  1. errors.Errorf() formats an error message and includes the stack trace. It's an equivalent fmt.Errorf(), but fmt.Errorf() doesn't print stack trace.
  2. errors.Wrapf() formats an error message and includes the stack trace. Another error is embedded in the message.
  3. The github.com/pkg/errors is no longer actively maintained. The repo has been archived so it is no longer accepting updates, but remains available for use.
@abhinav
Copy link
Collaborator

abhinav commented Nov 18, 2023

Thanks for the suggestion, @alex-semenyuk!
Sorry, it’s not clear what value the new section would provide.
With the availability of Go 1.13's error wrapping APIs, the functionality in pkg/errors is redundant—except the stack traces.
The maintainers of this style guide have historically advised against stacktrace-based errors, instead encouraging wrapping with fmt.Errorf to add context as needed. This is especially important in performance-critical code because acquiring a stack trace is not cheap1.
A section dedicated to pkg/errors would not be very helpful since it would just advise use of std errors.

Footnotes

  1. With pkg/errors, a common pattern I've seen is if err != nil { return errors.Wrap(err) } on every return site, which will re-capture a similar stack trace over and over again as the error travels up the call stack.

@alex-semenyuk
Copy link
Contributor Author

@abhinav Thanks for looking.
I think it will be helpful because it brings clarity why prefer to use fmt.Errorf if do not dig into history of wrapping feature and just look at the functionality it might be not so obvious which one prefer. Also thing like stack trace showing might be considered as additional point to use github.com/pkg/errors on first glance, but we can add your note/advice about stack trace based errors:

This is especially important in performance-critical code because acquiring a stack trace is not cheap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants