go-stacktrace
is a robust Go package for enhanced error handling, providing stack traces, error wrapping, and user-friendly error messages with optional color formatting.
- ๐ Detailed stack traces for improved debugging
- ๐ Custom user messages and payload support
- ๐จ Colorized error output (configurable)
- ๐ Error wrapping with context preservation
- ๐ชถ Lightweight with zero external dependencies
- ๐งช Comprehensive test coverage
go get github.com/Rahugg/go-stacktrace
import "github.com/Rahugg/go-stacktrace/errorhandler"
func main() {
err := someFunction()
if err != nil {
wrappedErr := errorhandler.WrapError(
err,
"operation-failed", // payload
"Failed to process request" // user message
)
fmt.Println(errorhandler.FailOnError(wrappedErr))
}
}
// Disable colored output
errorhandler.SetEnableColors(false)
// Enable colored output (default)
errorhandler.SetEnableColors(true)
The wrapped error includes:
- Original error message
- User-friendly message
- Custom payload (optional)
- Stack trace
- Color-coded output (optional)
Payload: operation-failed
Error Details
Original Error: file not found
User Message: Failed to process request
Stack Trace
main.someFunction
/path/to/file.go:25
main.main
/path/to/main.go:12
type TracedError struct {
OriginalError error
UserMessage string
Payload string
StackTrace []uintptr
}
- Red: Error messages
- Green: User messages
- Yellow: Payload information
- Blue: Function names in stack trace
- Magenta: Stack trace headers
- Cyan: Section headers
Contributions are welcome! Please feel free to submit a Pull Request. Or DM me.
This project is licensed under the MIT License - see the LICENSE file for details.
.
โโโ errorhandler
โ โโโ colors.go # Color constants
โ โโโ errorhandler.go # Core functionality
โ โโโ errorhandler_test.go # Test suite
โโโ example
โ โโโ main.go # Usage examples
โโโ go.mod
โโโ go.yml
โโโ README.md