Drop github.com/pkg/errors and use Golang errors package from standard library#219
Merged
Merged
Conversation
This is in preparation to switch away from "github.com/pkg/errors" (unmaintained), and replace it with Golang "errors" package. errors.WithStack() is specific to github.com/pkg/errors, and there's no equivalent in the Golang errors package. So the easiest thing we can do is to just stop using it. errors.WithStack() is mainly used in the database migration code, in order to have an abundance of logs in case something goes wrong. It's nice-to-have but not mandatory. Note that, in database/redis.go, we now print the error from Upgrade() with "%v" instead of "%+v": the purpose of "%+v" was to print the annotations added with errors.WithStack(), so it's no longer useful. errors.WithStack() is also used to log a GeoIPError: this error is a struct with a slice of errors (there can be a maximum of 2 errors, given that we load 2 databases). So the idea was to print these 2 errors thanks to errors.WithStack(). However that works only if you print the error with "%+v", which was not done, so I don't think it ever worked as intended. Again, nice-to-have but not mandatory, we can live without.
This is in preparation to switch away from "github.com/pkg/errors" (unmaintained), and replace it with Golang "errors" package. fmt.Errorf() was introduced in Go 1.13, making errors.Wrap() redundant, cf. https://tip.golang.org/doc/go1.13#error_wrapping.
At this point github.com/pkg/errors is no longer used so we can remove it.
This change improves readability IMO. errors.As was introduced in Go 1.13, and here are some references for the avid reader: * https://tip.golang.org/doc/go1.13#error_wrapping * https://go.dev/blog/go1.13-errors#examining-errors-with-is-and-as * https://pkg.go.dev/errors#As
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
github.com/pkg/errors is mostly obsolete, and the last relevant commit was in 2020: https://github.com/pkg/errors/commits/master/
In the meantime, Golang's errors package got better, and now that we raised the baseline to Go 1,18, it is a suitable replacement.
It just lacks
errors.WithStack(), but I think we can just stop using it.