I built a library to handle stack traces nicely in Go
Table of Contents
When writing error handling code in Go, you often want stack traces — but there aren’t many libraries that provide them in a nice, practical way.
For example:
pkg/errors
allows multipleerrors.Wrap
calls, but stack traces get duplicated. The maintenance has stopped, and newer APIs likeerrors.Join
are not supported.cockroachdb/errors
is well-designed, but the amount of information it outputs can be overwhelming and often requires additional processing.
With these challenges in mind, I created my own error handling library: stackprune/errors.
Goals and Design
- Provide
pkg/errors
compatible APIs (Wrap
,WithStack
, etc.) - Avoid duplicated stack traces even when wrapping multiple times
- Fully interoperable with Go’s standard
errors
package - Easy integration with structured logging tools like
slog
Error handling has been a long-discussed topic in Go. My goal was to find a “practical middle ground” that works well in real-world applications.
Usage
Here’s a simple example:
|
|
Internally, once WithStack
or New
is called, the stack trace is captured. When wrapped afterward, the same stack is preserved.
Quick Comparison
Library | Features |
---|---|
Go standard | Is /As are useful; stack traces require custom code |
pkg/errors | Compatible API, but deprecated; stack duplication |
stackprune/errors | Prevents stack duplication; integrates with slog |
Installation
|
|
Roadmap
- Improve GoDoc documentation
- Stabilize versioning
Conclusion
Error handling often becomes increasingly important as projects grow, but there are still few libraries that hit the right balance. I initially built stackprune/errors
for my own use case, but if others have similar needs, I’d be happy if you give it a try.
Author Eggpan
LastMod 2025-06-17