https://github.com/akishichinibu/safegroup
A panic-safe wrapper for Go's errgroup
https://github.com/akishichinibu/safegroup
errgroup golang
Last synced: 9 months ago
JSON representation
A panic-safe wrapper for Go's errgroup
- Host: GitHub
- URL: https://github.com/akishichinibu/safegroup
- Owner: akishichinibu
- License: mit
- Created: 2025-04-27T13:29:06.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-27T14:28:52.000Z (about 1 year ago)
- Last Synced: 2025-04-27T15:27:18.177Z (about 1 year ago)
- Topics: errgroup, golang
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# safegroup
`safegroup` is a wrapper around [golang.org/x/sync/errgroup](https://pkg.go.dev/golang.org/x/sync/errgroup), which automatically catches panics inside goroutines and converts them into errors, preventing the entire program from crashing.
ref: https://github.com/golang/go/issues/53757
## Features
- ✅ Compatible API with `errgroup`
- ✅ Captures stack trace information on panic
## Installation
```bash
go get github.com/akishichinibu/safegroup
```
## Usage
```go
package main
import (
"fmt"
"github.com/akishichinibu/safegroup"
)
func main() {
sg := safegroup.New()
sg.Go(func() error {
fmt.Println("this task runs normally")
return nil
})
sg.Go(func() error {
panic("something went wrong")
})
err := sg.Wait()
if err != nil {
fmt.Printf("SafeGroup caught an error: %v\n", err)
}
}
```
## Panic Handling
When a panic occurs inside a goroutine started by `Go` or `TryGo`, SafeGroup automatically recovers it and returns a `*PanicError` containing:
- The panic value (`Expt`)
- The captured stack trace (`Stack`)
You can safely log, report, or analyze panics without crashing your program.
### Example
```go
if pErr, ok := err.(*safegroup.PanicError); ok {
fmt.Printf("panic occurred: %v\nstack trace:\n%s\n", pErr.Expt, string(pErr.Stack))
}
```
## License
MIT License.
Parts of the test code are adapted from the Go project (BSD license).