Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.5 KB

README.md

File metadata and controls

49 lines (39 loc) · 1.5 KB

Errific

Errific Art

Super simple error strings in Go with caller prefix|suffix metadata, clean error wrapping, and helpful formatting methods.

Using New and Errorf to format an error message and handle error types.

var (
	ErrRegisterPet  errific.Err = "error registering pet"
	ErrValidateKind errific.Err = "only cats are allowed, cannot register '%s'"
)

func main() {
	if err := registerPet("hamster"); err != nil {
		switch {
		case errors.Is(err, ErrValidateKind): // 400 errors
			fmt.Println(http.StatusBadRequest, err)

		default: // 500 errors
			fmt.Println(http.StatusInternalServerError, err)
		}
	}
}

func registerPet(kind string) error {
	if err := validateKind(kind); err != nil {
		return ErrRegisterPet.New(err)
	}
	return nil
}

func validateKind(kind string) error {
	if kind != "cat" {
		return ErrValidateKind.Errorf(kind)
	}
	return nil
}
400 error registering pet [/tmp/sandbox4095574913/prog.go:30.registerPet]
only cats are allowed, cannot register 'hamster' [/tmp/sandbox4095574913/prog.go:37.validateKind]

Try it on the playground!

More to come! In the meantime look at the example tests.