Skip to content

🧨 Extensions for the standard errors package

License

Notifications You must be signed in to change notification settings

go-simpler/errorsx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

errorsx

checks pkg.go.dev goreportcard codecov

Extensions for the standard errors package.

📦 Install

Go 1.21+

go get go-simpler.org/errorsx

📋 Usage

IsAny

A multi-target version of errors.Is.

if errorsx.IsAny(err, os.ErrNotExist, os.ErrPermission) {
    fmt.Println(err)
}

HasType

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)
}

Split

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)
}

Close

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
}()