📝 Update WaitForError comment and add README.md for project documentation.

This commit is contained in:
Dan Jones 2025-11-13 14:53:54 -06:00
commit 4d92c7484d
2 changed files with 105 additions and 1 deletions

104
README.md Normal file
View file

@ -0,0 +1,104 @@
# waiterr
`waiterr` is a Go package that wraps `sync.WaitGroup` with enhanced error handling capabilities. It allows you to run multiple goroutines, collect any errors they return, and wait for their completion, either returning the first error encountered or aggregating all errors.
## Features
- **`Go(f func() error)`**: Runs a function `f` in a new goroutine, storing any error it returns.
- **`Wait() error`**: Waits for all goroutines to complete and returns a combined error of all non-nil errors.
- **`WaitForError() error`**: Waits for the first error to be returned by any goroutine and immediately returns that error. If all goroutines complete without error, it returns `nil`.
- **`Unwrap() []error`**: Returns a slice of all non-nil errors encountered by the goroutines.
## Installation
To install `waiterr`, use `go get`:
```bash
go get codeberg.org/danjones000/waiterr
```
## Usage
Here's a basic example of how to use `waiterr`:
```go
package main
import (
"errors"
"fmt"
"time"
"codeberg.org/danjones000/waiterr"
)
func main() {
we := new(waiterr.WaitErr)
we.Go(func() error {
time.Sleep(100 * time.Millisecond)
fmt.Println("Goroutine 1 finished")
return nil
})
we.Go(func() error {
time.Sleep(50 * time.Millisecond)
fmt.Println("Goroutine 2 finished with an error")
return errors.New("something went wrong in goroutine 2")
})
we.Go(func() error {
time.Sleep(150 * time.Millisecond)
fmt.Println("Goroutine 3 finished")
return nil
})
// Wait for all goroutines and get all errors
if err := we.Wait(); err != nil {
fmt.Printf("All goroutines finished. Combined error: %v
", err)
}
// You can also get the first error immediately
we2 := new(waiterr.WaitErr)
we2.Go(func() error {
time.Sleep(100 * time.Millisecond)
return errors.New("first error from we2")
})
we2.Go(func() error {
time.Sleep(50 * time.Millisecond)
return errors.New("second error from we2")
})
if err := we2.WaitForError(); err != nil {
fmt.Printf("First error from we2: %v
", err)
}
// Get all unwrapped errors
unwrappedErrors := we.Unwrap()
if len(unwrappedErrors) > 0 {
fmt.Println("Unwrapped errors:")
for i, err := range unwrappedErrors {
fmt.Printf(" %d: %v
", i+1, err)
}
}
}
```
## Contributing
Please refer to the [AGENTS.md](AGENTS.md) file for guidelines on contributing to this project, including code style, commit messages, and Git workflow.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Go Version
Go 1.25.3
## Dependencies
- `github.com/nalgeon/be v0.3.0` for testing.

View file

@ -47,7 +47,7 @@ func (we *WaitErr) Go(f func() error) {
}
// WaitForError waits for the first error to be returned by one of our go routines, and immediately returns
// with that error. If all functions return successfully, a nil is returned.
// with that error. If all functions return successfully, a nil is returned. It will panic if called before Go.
func (we *WaitErr) WaitForError() error {
if we.errCh == nil {
panic("WaitForError called before Go, errCh is nil")