chrismwendt / go-exceptions

Ergonomic error-handling in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-exceptions

Ergonomic error handling in Go. Check out this post for more details.

Example

Typical error-handling:

func f() error {
  err := g1()
  if err != nil {
    return err
  }

  v1, err := g2()
  if err != nil {
    return err
  }

  // ...
}

With go-exceptions:

import ex "github.com/chrismwendt/go-exceptions"

func f() (err error) {
  defer ex.Catch(&err)        // Catch() calls recover() and assigns the error to &err

  ex.Throw(g1())              // Throw() calls panic() if the error argument is not nil
  v1 := ex.Throw1(g2())       // Throw{1,2}() also return values
  v2 := ex.Throw1(g3(), "g3") // Throw() also accepts a label
  // ...
}

About

Ergonomic error-handling in Go


Languages

Language:Go 100.0%