|
7 | 7 | [](https://pkg.go.dev/github.com/kinbiko/rogerr?tab=doc) |
8 | 8 | [](https://github.com/kinbiko/rogerr/blob/master/LICENSE) |
9 | 9 |
|
10 | | -Consistent and greppable errors makes your logger and error reporting tools happy. This zero-dependency error handling support package for Go helps you achieve just that. |
| 10 | +A Go package for error handling with structured metadata. Zero dependencies. |
11 | 11 |
|
12 | | -[Blog post explaining the problem and the solution in detail](https://kinbiko.com/posts/2022-07-30-error-messages-should-be-boring/). |
| 12 | +[Blog post with detailed explanation](https://kinbiko.com/posts/2022-07-30-error-messages-should-be-boring/). |
13 | 13 |
|
14 | | -## Usage |
| 14 | +## Problem |
15 | 15 |
|
16 | | -When creating errors, **do not include goroutine-specific or request-specific information as part of the error message itself**. |
17 | | -Error messages with these specific bits of information often break filtering/grouping algorithms, e.g. as used by error reporting tools like Sentry/Rollbar/etc. (If you use Bugsnag, I recommend [kinbiko/bugsnag](https://github.com/kinbiko/bugsnag) for an **even better** experience than this package). |
| 16 | +Error messages that include unique data (user IDs, timestamps, etc.) break error grouping in monitoring tools like Sentry and Rollbar. |
18 | 17 |
|
19 | | -Instead this information should be treated as structured data, akin to structured logging solutions like Logrus and Zap. |
20 | | -In Go, it's conventional to attach this kind of request specific 'diagnostic' metadata to a `context.Context` type, and that's what this package enables too. |
| 18 | +## Solution |
21 | 19 |
|
22 | | -At a high level: |
| 20 | +Store unique data as structured metadata separate from the error message. |
| 21 | +This package attaches metadata to Go's `context.Context` and preserves it when wrapping errors. |
23 | 22 |
|
24 | | -1. Attach metadata to your context with `rogerr.WithMetadata` or `rogerr.WithMetadatum`. |
25 | | -1. When you come across an error, use `err = rogerr.Wrap(ctx, err, msg)` to attach the metadata accumulated so far to the wrapped error. |
26 | | -1. Return the error as you would normally, and at the time of logging/reporting, extract the metadata with `md := rogerr.Metadata(err)`. |
27 | | -1. Record the _structured_ metadata alongside the error message. |
| 23 | +## Usage |
28 | 24 |
|
29 | | -For more details, see [the official docs](https://pkg.go.dev/github.com/kinbiko/rogerr). |
| 25 | +1. Create an ErrorHandler: `handler := rogerr.NewErrorHandler()` |
| 26 | +2. Add metadata to context: `ctx = rogerr.WithMetadatum(ctx, "userID", 123)` |
| 27 | +3. Wrap errors with metadata: `err = handler.Wrap(ctx, err, "operation failed")` |
| 28 | +4. Extract metadata for logging: `metadata := rogerr.Metadata(err)` |
30 | 29 |
|
31 | | -### Build Recommendations |
| 30 | +### Build Options |
32 | 31 |
|
33 | | -For cleaner stacktrace file paths, build with the `-trimpath` flag: |
| 32 | +For cleaner stacktraces, use the `-trimpath` flag: |
34 | 33 |
|
35 | 34 | ```bash |
36 | 35 | go build -trimpath ./cmd/myapp |
37 | 36 | ``` |
38 | 37 |
|
39 | | -This removes local build path prefixes, showing module-relative paths instead of absolute machine-specific paths. |
40 | | -Not needed if stacktraces are disabled with `WithStacktrace(false)`, e.g. for performance reasons. |
| 38 | +This shows module-relative paths instead of absolute paths. |
| 39 | +Skip this if you disable stacktraces with `rogerr.WithStacktrace(false)`. |
| 40 | + |
| 41 | +[Full documentation](https://pkg.go.dev/github.com/kinbiko/rogerr) |
| 42 | + |
0 commit comments