errorx · Errors
Wrap errors, sequence operations, and collect concurrent failures.
Preserves standard error chains for errors.Is and errors.As.
Wrap an error
package main
import (
"errors"
"fmt"
"github.com/hainuoli/mino/core/errorx"
)
func main() {
notFound := errors.New("user not found")
err := errorx.Wrapf(notFound, "get user %d", 42)
fmt.Println(err)
fmt.Println(errors.Is(err, notFound))
}Complete example · cd examples && go run ./packages/errorx
get user 42: user not found
trueWrap(err, message) adds context; Wrapf formats it. Both return nil for a nil error.
Match several errors
func isExpected(err error) bool {
return errorx.In(err, os.ErrNotExist, context.Canceled)
}Checks each candidate with errors.Is, including wrapped errors.
Run operations in order
func execute(validate, save func() error) error {
return errorx.Chain(validate, save)
}Runs validate, then save if it succeeds. Returns immediately on the first error.
Collect concurrent errors
func runAll(tasks ...func() error) error {
var batch errorx.BatchError
var wg sync.WaitGroup
for _, task := range tasks {
wg.Add(1)
go func() {
defer wg.Done()
batch.Add(task())
}()
}
wg.Wait()
return batch.Err()
}Add ignores nil. NotNil reports whether errors have been collected. Err joins them with errors.Join, preserving errors.Is / errors.As.
Keep only the latest error
var latest errorx.AtomicError
latest.Set(err)
err = latest.Load()Both AtomicError and BatchError have usable zero values and support concurrent calls. AtomicError.Set(nil) leaves the stored error unchanged.