mustbe

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 2 Imported by: 14

README

Package mustbe simplifies error handling. mustbe.OK* functions receives error argument and panics if is is not nil. mustbe.Catched/mustbe.CatchedAs functions handle these (and only these) panics.

See documentation on GoDoc

License: MIT

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

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

func CatchedAsAnnotated(targetError *error, format string)

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

func OKOr(err error, errs ...error) error

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

func OKOrIs(err error, errs ...error) error

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 Thrown

func Thrown(err error)

Thrown is the synonym of OK

func True added in v1.3.0

func True(test bool, err error)

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

func (ErrorBag) Unwrap added in v1.2.0

func (e ErrorBag) Unwrap() error

Unwrap returns a error wrapped by ErrorBag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL