A Go package that provides breadth-first error search utilities.
It extends the standard errors.As semantics to traverse
error trees breadth-first instead of depth-first.
- Breadth-first traversal of wrapped error trees
- Works with both
Unwrap() errorandUnwrap() []error - Compatible with Go’s
errors.Asand customAs(any) boolmethods - Ideal for finding “higher-level” causes before deep leaves
The following example will set e as customErr{"target"}
func ExampleAsType() {
err := fmt.Errorf("wrap 1: %w",
fmt.Errorf("wrap 2: %w",
customErr{"target too deep"},
),
)
err2 := fmt.Errorf("wrap 1: %w", customErr{"target"})
if e, ok := bfserr.AsType[customErr](errors.Join(err, err2)); ok {
fmt.Println("found:", e)
}
}go get github.com/Brian-Williams/bfserrIf you can use errors.As you should. In most common error trees it will outperform bfserr.AsType.
In the case where you need a bfs of an error tree this will have comparable or better performance to errors.
BenchmarkAs-10 8333600 121.9 ns/op
BenchmarkAsType-10 7887480 149.2 ns/op
BenchmarkAsDeep-10 5703589 203.5 ns/op 40 B/op 2 allocs/op
BenchmarkAsTypeDeep-10 12341869 96.14 ns/op 104 B/op 4 allocs/op