Skip to content

Commit dc84fb5

Browse files
authored
Update docs (#13)
* docs: Prefer handler over package-level functions * chore: update examples * docs: Remove plug for bugsnag The maturity of the two packages are now more equal than before * docs: Improve writing in the README
1 parent 231da60 commit dc84fb5

3 files changed

Lines changed: 26 additions & 20 deletions

File tree

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,36 @@
77
[![Go Documentation](http://img.shields.io/badge/godoc-documentation-blue.svg?style=flat)](https://pkg.go.dev/github.com/kinbiko/rogerr?tab=doc)
88
[![License](https://img.shields.io/github/license/kinbiko/rogerr.svg?style=flat)](https://github.com/kinbiko/rogerr/blob/master/LICENSE)
99

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

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/).
1313

14-
## Usage
14+
## Problem
1515

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

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
2119

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

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
2824

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

31-
### Build Recommendations
30+
### Build Options
3231

33-
For cleaner stacktrace file paths, build with the `-trimpath` flag:
32+
For cleaner stacktraces, use the `-trimpath` flag:
3433

3534
```bash
3635
go build -trimpath ./cmd/myapp
3736
```
3837

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+

doc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*Package rogerr is a zero-dependency error handling support package.
1+
/*
2+
Package rogerr is a zero-dependency error handling support package.
23
34
When creating errors, **do not include goroutine-specific or request-specific information as part of the error message itself**.
45
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).
@@ -8,8 +9,9 @@ In Go, it's conventional to attach this kind of request specific 'diagnostic' me
89
910
At a high level:
1011
12+
1. Create an ErrorHandler with `handler := rogerr.NewErrorHandler()`.
1113
1. Attach metadata to your context with `rogerr.WithMetadata` or `rogerr.WithMetadatum`.
12-
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.
14+
1. When you come across an error, use `err = handler.Wrap(ctx, err, msg)` to attach the metadata accumulated so far to the wrapped error.
1315
1. Return the error as you would normally, and at the time of logging/reporting, extract the metadata with `md := rogerr.Metadata(err)`.
1416
1. Record the _structured_ metadata alongside the error message.
1517
*/

example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
func ExampleWrap() {
11+
handler := rogerr.NewErrorHandler()
12+
1113
someFuncWithAProblem := func(_ context.Context) error {
1214
return fmt.Errorf("some low level err")
1315
}
@@ -18,7 +20,7 @@ func ExampleWrap() {
1820

1921
err := someFuncWithAProblem(ctx)
2022
if err != nil {
21-
return rogerr.Wrap(ctx, err, "wrap args")
23+
return handler.Wrap(ctx, err, "wrap args")
2224
}
2325
return nil
2426
}

0 commit comments

Comments
 (0)