https://github.com/lestrrat-go/rescue
https://github.com/lestrrat-go/rescue
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lestrrat-go/rescue
- Owner: lestrrat-go
- License: mit
- Created: 2021-06-01T14:26:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-13T05:20:28.000Z (almost 4 years ago)
- Last Synced: 2025-01-09T03:41:45.726Z (6 months ago)
- Language: Go
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rescue
Aggregate errors/panics from goroutines
## Auto-capture panics (single goroutine)
```go
func Foo(ctx context.Context) {
defer rescue.Do(ctx)
...
}r := rescue.New()
go Foo(r.Context(ctx))r.Wait()
if err := r.Error(ctx); err != nil {
... handle panic ...
}
```## Auto-capture errors (single goroutine)
```go
func Foo(ctx context.Context) (err error){
rescue.Bind(ctx, &err)
defer rescue.Do(ctx)
...
}r := rescue.New()
go Foo(r.Context(ctx))r.Wait()
if err := r.Error(ctx); err != nil {
// Because this shares the interface panics,
// we need to convert types
switch err := err.(type) {
case error:
... handle error ...
default:
... handle panic ...
}
}
```## Capture errors and panics within multiple goroutines
```go
func Foo(ctx context.Context) (err error){
rescue.Bind(ctx, &err)
defer rescue.Do(ctx)
...
}rg := rescue.NewGroup()
for i := 0; i < 10; i++ {
r := rg.New()
go Foo(r.Context(ctx))
}rg.Wait()
for err := rage rg.Errors() {
// Because this shares the interface panics,
// we need to convert types
switch err := err.(type) {
case error:
... handle error ...
default:
... handle panic ...
}
}