-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sentinel errors should not have stack traces (#2042)
## Problem While debugging a program which is using badger I noticed that stack traces returned from my code (which uses errors with stack traces) are useless. I found the reason: badger uses sentinel errors (great!) but it creates them with stack traces at the "global init time" (e.g., `var ErrFoo = errors.New(...)`) and not at "return from function time". Because of that I was seeing stack traces like: ``` github.com/dgraph-io/badger/v4.init /go/pkg/mod/github.com/dgraph-io/badger/[email protected]/compaction.go:41 runtime.doInit1 /usr/local/go/src/runtime/proc.go:6740 runtime.doInit /usr/local/go/src/runtime/proc.go:6707 runtime.main /usr/local/go/src/runtime/proc.go:249 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1650 ``` Instead of a stack trace which would show me where the call which is returning sentinel error is made. ## Solution Ideally, one would create all sentinel errors without stack traces (standard errors package works great for that) and then when the error happens in a function, you return `errors.WithStack(ErrFoobar)` instead of just `ErrFoobar`. This means that sentinel error is then annotated with a stack trace at the place where the error happens. But if you do that, then you have to check for sentinel errors with `errors.Is` instead of just comparing errors with equality (`==`). That might be a breaking change for people who are not using `errors.Is` as they should. Because badger's own codebase uses equality instead of `errors.Is` I decided to not do this in this PR, but instead: Just make sentinel errors without stack traces. This allows me to annotate with a stack trace inside my program (otherwise my program does not do so because if finds an existing stack trace and assumes it is a deeper one) and I get at least that part of the stack trace. Equality still works as usual. I suggest this is merged this way and in v5 of badger `errors.WithStack` and required `errors.Is` are introduced. ## Side note Some time ago [I made this errors package for Go](https://gitlab.com/tozd/go/errors) which fixes various issues in `github.com/pkg/errors` and also has direct support for sentinel (in that package called "base") errors. It also has some features badger has in its `y` package for dealing with errors. You could consider adopting that package to have a comprehensive errors handling in badger.
- Loading branch information
Showing
9 changed files
with
47 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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