Extensions for the standard errors
package.
Go 1.21+
go get go-simpler.org/errorsx
A multi-target version of errors.Is
.
if errorsx.IsAny(err, os.ErrNotExist, os.ErrPermission) {
fmt.Println(err)
}
Reports whether the error has type T
.
It is equivalent to errors.As
without the need to declare the target variable.
if errorsx.HasType[*os.PathError](err) {
fmt.Println(err)
}
Returns errors joined by errors.Join
or by fmt.Errorf
with multiple %w
verbs.
If the given error was created differently, Split
returns nil.
if errs := errorsx.Split(err); errs != nil {
fmt.Println(errs)
}
Attempts to close the given io.Closer
and assigns the returned error (if any) to err
.
func() (err error) {
f, err := os.Open("file.txt")
if err != nil {
return err
}
defer errorsx.Close(f, &err)
return nil
}()