Skip to content

Golang string errors with caller information, and format methods.

License

Notifications You must be signed in to change notification settings

leefernandes/errific

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Golang string errors with caller information, and format methods.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages