An open API service indexing awesome lists of open source software.

https://github.com/rahugg/go-stacktrace

go-stacktrace is a lightweight and customizable Go package for enhanced error handling and tracing. It wraps errors with additional context, user-friendly messages, and detailed stack traces, making it easier to diagnose and debug issues in your Go applications.
https://github.com/rahugg/go-stacktrace

error-handling error-traces golang golang-package golang-tools lightweight traceroute tracing-library zero-dependency

Last synced: about 1 month ago
JSON representation

go-stacktrace is a lightweight and customizable Go package for enhanced error handling and tracing. It wraps errors with additional context, user-friendly messages, and detailed stack traces, making it easier to diagnose and debug issues in your Go applications.

Awesome Lists containing this project

README

          

# go-stacktrace

`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.

## Features

- ๐Ÿ” 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

## Installation

```bash
go get github.com/Rahugg/go-stacktrace
```

## Usage

### Basic Error Wrapping

```go
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))
}
}
```

### Controlling Color Output

```go
// Disable colored output
errorhandler.SetEnableColors(false)

// Enable colored output (default)
errorhandler.SetEnableColors(true)
```

### Error Information

The wrapped error includes:
- Original error message
- User-friendly message
- Custom payload (optional)
- Stack trace
- Color-coded output (optional)

## Example Output

```
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
```

## Features in Detail

### TracedError Structure
```go
type TracedError struct {
OriginalError error
UserMessage string
Payload string
StackTrace []uintptr
}
```

### Available Colors
- Red: Error messages
- Green: User messages
- Yellow: Payload information
- Blue: Function names in stack trace
- Magenta: Stack trace headers
- Cyan: Section headers

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.
Or DM me.

## License

This project is licensed under the MIT License - see the LICENSE file for details.

## Project Structure
```
.
โ”œโ”€โ”€ 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
```