Documentation
¶
Overview ¶
Package mustbe simplifies error handling. mustbe.OK* functions receives error argument and panics if is is not nil. mustbe.Catched function handle these (and only these) panics.
Index ¶
- func Catched(cfun func(error))
- func CatchedAs(targetError *error)
- func CatchedAsAnnotated(targetError *error, format string)
- func OK(err error)
- func OKOr(err error, errs ...error) error
- func OKOrIs(err error, errs ...error) error
- func OKVal(val interface{}, err error) interface{}
- func Thrown(err error)
- func True(test bool, err error)
- type ErrorBag
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Catched ¶
func Catched(cfun func(error))
Catched is a function for defer'ed use (see OK example). Errors thrown by mustbe.OK* functions passes to cfun, other panic's are re-panics.
func CatchedAs ¶
func CatchedAs(targetError *error)
CatchedAs catches mustbe.* error and assigns it to the targetError. It is useful if we just need to return catched error from function.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
foo := func() (err error) {
defer mustbe.CatchedAs(&err)
mustbe.OK(errors.New("sample error"))
return nil
}
err := foo()
fmt.Println("Returned", err)
}
Output: Returned sample error
func CatchedAsAnnotated ¶ added in v1.4.0
CatchedAs catches mustbe.* error, wraps it using the `fmt.Errorf(format, err)` and assigns the result to the targetError. The format string must contain a `%w` placeholder for proper fmt.Errorf work.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
foo := func() (err error) {
defer mustbe.CatchedAsAnnotated(&err, "wrapped %w")
mustbe.OK(errors.New("sample error"))
return nil
}
err := foo()
fmt.Println("Returned", err)
}
Output: Returned wrapped sample error
func OK ¶
func OK(err error)
OK throws panic if err != nil
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer mustbe.Catched(func(err error) {
fmt.Println("Catched", err)
})
err := errors.New("sample error")
mustbe.OK(err)
fmt.Println("Will not be printed")
}
Output: Catched sample error
func OKOr ¶
OKOr throws panic if err not nil and not any of errs, oterwise returns err. Deprecated: It is recommended to use mustbe.OKOrIs instead.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer mustbe.Catched(func(err error) {
fmt.Println("Catched", err)
})
var (
err error
goodErr = errors.New("good error")
badErr = errors.New("bad error")
)
err = goodErr
fmt.Println(mustbe.OKOr(err, goodErr))
err = badErr
fmt.Println(mustbe.OKOr(err, goodErr))
}
Output: good error Catched bad error
func OKOrIs ¶ added in v1.4.0
OKOrIs throws panic if err not nil and not "Is" (as in errors.Is) any of errs, oterwise returns err
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer mustbe.Catched(func(err error) {
fmt.Println("Catched", err)
})
var (
err error
goodErr = errors.New("good error")
wrappedGoodErr = fmt.Errorf("wrapped %w", goodErr)
badErr = errors.New("bad error")
)
err = goodErr
fmt.Println(mustbe.OKOrIs(err, goodErr))
err = wrappedGoodErr
fmt.Println(mustbe.OKOrIs(err, goodErr))
err = badErr
fmt.Println(mustbe.OKOrIs(err, goodErr))
}
Output: good error wrapped good error Catched bad error
func OKVal ¶
func OKVal(val interface{}, err error) interface{}
OKVal throws panic if err != nil, oterwise returns val
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer mustbe.Catched(func(err error) {
fmt.Println("Catched", err)
})
divide := func(x, y int) (int, error) {
if y == 0 {
return 0, errors.New("division by zero")
}
return x / y, nil
}
fmt.Println("4 / 2 =", mustbe.OKVal(divide(4, 2)).(int))
fmt.Println("4 / 0 =", mustbe.OKVal(divide(4, 0)).(int)) // will not be printed
}
Output: 4 / 2 = 2 Catched division by zero
func True ¶ added in v1.3.0
True throws panic if test is not true.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer mustbe.Catched(func(err error) {
fmt.Println("Catched:", err)
})
n := 42
mustbe.True(n == 42, errors.New("not a 42"))
fmt.Println("Equality to 42 was verified")
mustbe.True(n == 43, errors.New("not a 43"))
fmt.Println("Equality to 43 was verified")
}
Output: Equality to 42 was verified Catched: not a 43
Types ¶
type ErrorBag ¶ added in v1.2.0
type ErrorBag struct {
// contains filtered or unexported fields
}
ErrorBag is a wrapper around the error value. All OK*/Thrown functions are panics with the ErrorBag value. This type is useful for manual panic recovering.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/davidmz/mustbe"
)
func main() {
defer func() {
if pnc := recover(); pnc != nil {
if errBag, ok := pnc.(mustbe.ErrorBag); ok {
fmt.Println("Wrapped error:", errBag.Unwrap())
}
}
}()
mustbe.Thrown(errors.New("sample error"))
}
Output: Wrapped error: sample error