Skip to content

Commit fee649f

Browse files
authored
docs: document stdlib re-exports in errors package (#71)
* docs: document stdlib re-exports in errors package Add sections for Is, As, Unwrap, Join, and Cause to the errors howto page now that the errors package re-exports all stdlib error functions. Update the Packages page description to reflect drop-in replacement. * fix: address PR review feedback - Add missing imports (database/sql, fmt) to Checking errors example - Clarify that error notification is in the notifier sub-package * fix: address round 2 review feedback - Clarify that Cause is ColdBrew-specific, not a stdlib re-export - Wrap example in a function so err is defined - Remove fmt.Println from standalone snippet
1 parent 3012603 commit fee649f

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

Packages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Interceptors provides a common set of reusable interceptors for grpc services
3535
Documentation can be found at [interceptor-docs]
3636

3737
## [Errors]
38-
errors provides an implementation of golang error with stack strace information attached to it, the error objects created by this package are compatible with https://golang.org/pkg/errors/
38+
A drop-in replacement for the standard `errors` package that adds stack trace capture and gRPC status codes. Standard library helpers (`Is`, `As`, `Unwrap`, `Join`) are re-exported. Error notification is provided by the [Notifier] sub-package below.
3939

4040
Documentation can be found at [errors-docs]
4141

howto/errors.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func somefunction() error {
5959
The ColdBrew [errors package] provides a `Wrap` function that can be used to wrap an error with a message. This is useful when you want to add more context to an error.
6060

6161
```go
62-
6362
import (
6463
"github.com/go-coldbrew/errors"
6564
)
@@ -76,6 +75,67 @@ func function2() error {
7675
}
7776
```
7877

78+
### Checking errors
79+
80+
Standard library error helpers (`Is`, `As`, `Unwrap`, `Join`) are re-exported, so you don't need a separate `import "errors"`. Use `errors.Is` to check if an error matches a sentinel value, and `errors.As` to extract a specific error type from the chain:
81+
82+
```go
83+
import (
84+
"database/sql"
85+
"fmt"
86+
87+
"github.com/go-coldbrew/errors"
88+
)
89+
90+
func handleQuery(err error) {
91+
// Check if an error matches a sentinel value
92+
if errors.Is(err, sql.ErrNoRows) {
93+
// handle not found
94+
}
95+
96+
// Extract a specific error type from the chain
97+
var ext errors.ErrorExt
98+
if errors.As(err, &ext) {
99+
fmt.Println("gRPC code:", ext.GRPCStatus().Code())
100+
}
101+
}
102+
```
103+
104+
### Finding root cause
105+
106+
ColdBrew also provides `errors.Cause` to walk the `Unwrap` chain and find the root cause error. This works on any error, not just ColdBrew errors:
107+
108+
```go
109+
root := errors.Cause(err) // returns the innermost error in the chain
110+
```
111+
112+
For ColdBrew errors, you can also use the `Cause()` method on the `ErrorExt` interface, which returns the same result.
113+
114+
### Combining errors
115+
116+
Use `errors.Join` to combine multiple errors into a single error:
117+
118+
```go
119+
err1 := errors.New("first problem")
120+
err2 := errors.New("second problem")
121+
combined := errors.Join(err1, err2)
122+
123+
// Both errors are preserved in the chain
124+
errors.Is(combined, err1) // true
125+
errors.Is(combined, err2) // true
126+
```
127+
128+
### Unwrapping errors
129+
130+
Use `errors.Unwrap` to get the immediate parent error in the chain:
131+
132+
```go
133+
base := errors.New("base")
134+
wrapped := errors.Wrap(base, "context")
135+
136+
inner := errors.Unwrap(wrapped) // returns base
137+
```
138+
79139
## ColdBrew notifier package
80140

81141
The ColdBrew [notifier package] provides a simple way to notify errors to different notifiers like [Sentry], [Airbrake], [Rollbar] etc.

0 commit comments

Comments
 (0)