adverr/README.md
Timon Ringwald 9504c8a01b added README
2020-09-09 11:55:13 +02:00

3.7 KiB

adverr

-- import "adverr"

Package adverr implements errors with call stack traces as well as error templates for error equality

Usage examples

Creating templates:

var (
	ErrDoStuffFailed = adverr.NewErrTmpl("ErrDoStuffFailed", "Could'nt do stuff because of %s")
)

Creating independent error (without error template):

func doStuffWithIndependentErr() error {
	return adverr.New("Could'nt do stuff")
}

Creating error based on template:

func doStuff() error {
	return ErrDoStuffFailed.New("reasons")
}

Printing errors on stderr convieniently:

Print(myErr)
Println(myErr)

Printing errors on stderr and exit with exitcode:

Fatal(myErr, 1)
Fatalln(myErr, 1)

Advantages of error templates

two errors made by the same template will return true when called with errors.Is()

Usage

var CallStackLength int = 100

CallStackLength decides how many calls from the call stack should be gathered at most

var (
	// ErrTmplUsedAsErr is returned from ErrTmpl.Error() because an error template should never be used as an actual error value
	ErrTmplUsedAsErr = NewErrTmpl("ErrTmplUsedAsErr", "Error template used as error value: %s")
)
var TraceCallStack bool = true

TraceCallStack decides if any call stack traces will be gathered when creating Errors If your application is doing performance-heavy tasks with lots of Error creations, you may consider setting this to false If set to false, all CallTraces in Error will be nil

func Fatal

func Fatal(err error, exitcode int)

Fatal prints the given err to stderr and exits immediately with the given exit code

func Fatalln

func Fatalln(err error, exitcode int)

Fatalln prints the given err to stderr followed by a newline and exits immediately with the given exit code

func Print

func Print(err error)

Print prints the given err to stderr

func Println

func Println(err error)

Println prints the given err to stderr followed by a newline

type CallTrace

type CallTrace struct {
}

CallTrace represents a call stack trace similar to Java's stack trace

func Trace

func Trace(skip int) *CallTrace

Trace returns a new CallTrace starting from this call Use skip to skip the first entries in the trace

func (*CallTrace) String

func (ct *CallTrace) String() string

type ErrTmpl

type ErrTmpl struct {
}

ErrTmpl is an error template to define equalities between different errors

func NewErrTmpl

func NewErrTmpl(name, format string) *ErrTmpl

NewErrTmpl returns a new error template with the given format string as a predefined error message

func (*ErrTmpl) Error

func (t *ErrTmpl) Error() string

Error implementation just for satisfying the error interface Please dont use ErrTmpls as actual errors

func (*ErrTmpl) New

func (t *ErrTmpl) New(args ...interface{}) *Error

New returns a new Error in which the given values are being formatted into the format string of its template

type Error

type Error struct {
}

Error is a wrapper for error with stack trace

func New

func New(msg string) *Error

New returns a new Error with the given message

func Wrap

func Wrap(msg string, cause error) *Error

Wrap returns a new Error with the given message which is caused by cause

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is implements the error equality function used by errors.Is() It returns true if the error is the same instance or is created using the same ErrTmpl

func (*Error) Unwrap

func (e *Error) Unwrap() error