Skip to content

Commit e303732

Browse files
committed
docs(README): add section on multiple error handling with examples for better clarity and usage guidance
1 parent 0deaf2f commit e303732

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Enhanced error handling for Go with stack traces, contextual values, and structu
1111
- **Rich Stack Traces**: Automatic capture with `github.com/pkg/errors` compatibility
1212
- **Contextual Variables**: Attach key-value pairs to errors for better debugging
1313
- **Type-Safe Values**: Compile-time type checking for error context using generics
14+
- **Multiple Error Handling**: Aggregate and manage multiple errors with `goerr.Errors`
1415
- **Error Categorization**: Tag-based error classification for different handling strategies
1516
- **Structured Logging**: Native `slog` integration with recursive error details
1617
- **Error Identity**: ID-based error comparison for flexible error matching
@@ -81,6 +82,33 @@ if goErr := goerr.Unwrap(err); goErr != nil {
8182
}
8283
```
8384

85+
### Multiple Error Handling
86+
87+
Aggregate multiple errors with `goerr.Errors`:
88+
89+
```go
90+
// Collect errors during processing
91+
var errs *goerr.Errors
92+
for _, item := range items {
93+
if err := processItem(item); err != nil {
94+
errs = goerr.Append(errs, err) // nil-safe
95+
}
96+
}
97+
98+
// Return only if errors occurred
99+
return errs.ErrorOrNil() // nil if no errors
100+
101+
// Join errors directly
102+
combined := goerr.Join(err1, err2, err3)
103+
104+
// All errors displayed together
105+
fmt.Printf("%v", combined)
106+
// Output: error1\nerror2\nerror3
107+
108+
// Works with standard library
109+
if errors.Is(combined, err1) { /* true */ }
110+
```
111+
84112
### Contextual Data
85113

86114
**String-based Values**
@@ -295,6 +323,7 @@ jsonData, _ := json.Marshal(err)
295323
See the [examples](./examples) directory for complete working examples:
296324
- Stack trace handling
297325
- Contextual variables
326+
- Multiple error aggregation
298327
- HTTP error responses
299328
- Sentry integration
300329
- Structured logging with slog

0 commit comments

Comments
 (0)