errific

package module
v0.0.0-...-782f5d7 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

View Source
const (
	// Suffix adds caller information at the end of the error message.
	// This is default.
	Suffix callerOption = iota
	// Prefix adds caller information at the beginning of the error message.
	Prefix
	// Disabled does not include caller information in the error message.
	Disabled
)
View Source
const (
	// Newline joins errors with \n.
	// This is default.
	Newline layoutOption = iota
	// Inline wraps errors with ↩.
	Inline
)
View Source
const (
	// Trim current working directory from filenames.
	TrimCWD trimCWDOption = true
)
View Source
const (
	// Include stacktrace in error message.
	WithStack withStackTraceOption = true
)

Variables

View Source
var (
	// TrimPrefixes from caller frame filenames.
	TrimPrefixes = func(prefixes ...string) trimPrefixesOption {
		return trimPrefixesOption{prefixes: prefixes}
	}
)

Functions

func Configure

func Configure(opts ...Option)

Configure errific options.

Types

type Err

type Err string

Err string type.

To include runtime caller information on the error, one of the Err methods, other than Error(), must be called.

For examples see the example tests. All examples demonstrate using exported errors as a recommended best practice because exported errors enable unit-tests that assert expected errors such as: assert.ErrorIs(t, err, ErrProcessThing).

func (Err) Error

func (e Err) Error() string

func (Err) Errorf

func (e Err) Errorf(a ...any) errific

Errorf returns an error using Err formatted as text. Use Errorf if your Err string itself contains fmt format specifiers.

var ErrProcessThing errific.Err = "error processing thing id: '%s'"

return ErrProcessThing.Errorf("abc")

func (Err) New

func (e Err) New(errs ...error) errific

New returns an error using Err as text with errors joined.

var ErrProcessThing errific.Err = "error processing a thing"

return ErrProcessThing.New(err)

func (Err) Withf

func (e Err) Withf(format string, a ...any) errific

Withf returns an error with a formatted string inline to Err as text.

var ErrProcessThing errific.Err = "error processing thing"

return ErrProcessThing.Withf("id: '%s'", "abc")

func (Err) Wrapf

func (e Err) Wrapf(format string, a ...any) errific

Wrapf return an error using Err as text and wraps a formatted error. Use Wrapf to format an error and wrap it.

var ErrProcessThing errific.Err = "error processing thing"

return ErrProcessThing.Wrapf("cause: %w", err)

type Option

type Option interface {
	ErrificOption()
}

Jump to

Keyboard shortcuts

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