|
| 1 | +# stackerr |
| 2 | + |
| 3 | +`stackerr` is a lightweight Go library for panic recovery and error handling with customizable stack traces. It simplifies debugging by capturing clean, readable stack traces when panics occur or when errors are explicitly thrown. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Panic Recovery**: Easily recover from panics and wrap them into errors with stack traces. |
| 8 | +- **Clean Stack Traces**: Automatically strips noisy arguments and simplifies file paths (e.g., replacing `$GOPATH` with `[Proj]`). |
| 9 | +- **Customizable**: Configure which strings to strip from function names, define custom path replacements, and set stack depth limits. |
| 10 | +- **Error Wrapping**: Supports standard Go error wrapping (`Unwrap()`). |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +go get github.com/LZStock-OS/stackerr |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +### Basic Recovery |
| 21 | + |
| 22 | +Use `stackerr.Recover` in a `defer` statement to catch panics and populate an error return variable. |
| 23 | + |
| 24 | +```go |
| 25 | +package main |
| 26 | + |
| 27 | +import ( |
| 28 | + "fmt" |
| 29 | + "github.com/LZStock-OS/stackerr" |
| 30 | +) |
| 31 | + |
| 32 | +func riskyOperation() (err error) { |
| 33 | + defer stackerr.Recover(&err) |
| 34 | + |
| 35 | + // Simulating a panic |
| 36 | + panic("something went wrong") |
| 37 | +} |
| 38 | + |
| 39 | +func main() { |
| 40 | + if err := riskyOperation(); err != nil { |
| 41 | + fmt.Printf("Caught error: %v\n", err) |
| 42 | + |
| 43 | + if stack := stackerr.GetStack(err); stack != "" { |
| 44 | + fmt.Println("Stack Trace:") |
| 45 | + fmt.Println(stack) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Throwing Errors with Stack Traces |
| 52 | + |
| 53 | +Use `stackerr.ThrowPanic` to explicitly panic with a wrapped error containing the current stack trace. |
| 54 | + |
| 55 | +```go |
| 56 | +func doSomething() { |
| 57 | + err := someInternalFunction() |
| 58 | + if err != nil { |
| 59 | + // This will panic and be caught by a defer stackerr.Recover up the chain |
| 60 | + stackerr.ThrowPanic(fmt.Errorf("operation failed: %w", err)) |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Configuration |
| 66 | + |
| 67 | +You can customize the library's behavior by modifying the global `stackerr.Config` variable. It is recommended to do this in your `init()` function or main setup. |
| 68 | + |
| 69 | +```go |
| 70 | +import ( |
| 71 | + "os" |
| 72 | + "github.com/LZStock-OS/stackerr" |
| 73 | +) |
| 74 | + |
| 75 | +func init() { |
| 76 | + // Customize string sequences to strip from function names |
| 77 | + stackerr.Config.StripSequences = []string{"(0", "({", "(*"} |
| 78 | + |
| 79 | + // Set maximum stack depth (default: 10) |
| 80 | + stackerr.Config.MaxStackDepth = 20 |
| 81 | + |
| 82 | + // Customize path replacements to hide sensitive paths or make logs shorter |
| 83 | + stackerr.Config.PathReplacements = map[string]string{ |
| 84 | + "/home/user/go/src/": "[Src]", |
| 85 | + "github.com/my/project/": "", |
| 86 | + } |
| 87 | + |
| 88 | + // Redirect log output (default: os.Stderr) |
| 89 | + // Set to nil to disable automatic logging on recovery |
| 90 | + stackerr.Config.Output = os.Stdout |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## API Reference |
| 95 | + |
| 96 | +### `func Recover(err *error)` |
| 97 | + |
| 98 | +Catches panics, wraps them in a `callStackErr`, and assigns it to `*err`. It also logs the panic and stack trace to the configured output. |
| 99 | + |
| 100 | +### `func ThrowPanic(err error)` |
| 101 | + |
| 102 | +Wraps the given error with the current stack trace and panics. Use this when you want to abort execution but preserve context for the recovery handler. |
| 103 | + |
| 104 | +### `func GetStack(err error) string` |
| 105 | +Retrieves the stack trace from an error if it was created by `Recover` or `ThrowPanic`. Returns an empty string otherwise. |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +MIT |
0 commit comments