errors

package
v0.0.0-...-5c1fc54 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 9 Imported by: 0

README

Error handling policy

Handling errors from vendor functions

If you are handling an error in a vendor library, it is necessary to use errors.Wrap() so that you capture a stacktrace at that time.

result, err := vendorlib.DoTheirThing()
if err != nil {
    return errors.Wrap(err, "unable to do their thing")
}

Handling errors from local functions

If you are handling an error from a local function, use errors.WithMessage() to add a message. Do not use errors.Wrap() or you will overwrite the stacktrace that's already been captured.

result, err := DoOurThing()
if err != nil {
    return errors.WithMessage(err, "unable to do our thing")
}

Do not pass errors without adding a message

Avoid this pattern, as it makes the application harder to debug:

result, err := DoOurThing()
if err != nil {
    return err
}

Handling errors that happen in a loop

Unless an error in a loop should crash the application, you should log it and continue. Unhandled panic can be caught with a deferred function:

for {
    func() {
        // In case of panic, don't exit the loop, just log and continue
        defer func() {
            err := recover()
            if err != nil {
                s.log.Error(err)
            }
        }()

        result, err := DoOurThing()

        // Log returned error and stacktrace, and also add an error message
        if err != nil {
            errors.LogError(s.log, err).Error("unable to do our thing")
            continue
        }

        // ...
    }()
}

Log an error that's bubbled up to the top

If the error has bubbled up to the top and should be fatal, such as errors during application initialization, log the error using errors.LogError(), then stop the application with a non-zero exit code:

err := rootCmd.Execute()
if err != nil {
    errors.Fatal(log, errors.Wrap(err, "app failed to run"))
}

or create your own error:

if configValue != "expected" {
    errors.Fatal(log, errors.Errorv("unexpected config value", configValue))
}

Add contextual information to an error

Use errors.Errorv(), errors.Wrapv(), and errors.WithMessagev() to add add contextual information to the end of the error string

foo = "bar"
foo2 = "bar2"

errors.Errorv("error string", foo)              // error string (bar)
errors.Errorv("error string", foo, foo2)        // error string (bar, bar2)
errors.Errorv("error string", logrus.Fields{    // error string (foo=bar, foo2=bar2)
    "foo": foo,
    "foo2": foo2,
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CatchPanicAndLogError

func CatchPanicAndLogError(log logg.Logg, logMsg string)

Catch panic and log it

Usage:
func main() {
    defer errors.CatchPanicAndLogIt(log)
    panic("this was inevitable") // logged: "panic caught: this was inevitable"
}

func CatchPanicAndLogWarning

func CatchPanicAndLogWarning(log logg.Logg, logMsg string)

func CatchPanicDo

func CatchPanicDo(doFunc DoWithErrFunc)

Catch panic, convert it to an error object, and do something with it

Usage:
func main() {
    defer errors.CatchPanicDo(func(err error) {
        fmt.Print(err.Error()) // "panic caught: this was inevitable"
    })
    panic("this was inevitable")
}

func CatchPanicSetErr

func CatchPanicSetErr(err *error, message string)

Catch panic, convert it to an error object, and set an error pointer with it with a message

Usage
func do() (err error) {
    defer errors.CatchPanicSetErr(&err, "something happened")
    panic("this was inevitable")
}
func main() {
    if err := do(); err != nil {
        fmt.Print(err.Error()) // "something happened: panic caught: this was inevitable"
    }
}

func CatchPanicValueDo

func CatchPanicValueDo(panicHandle DoWithPanicFunc)

Catch panic and do something with it

Usage:
func main() {
    defer errors.CatchPanicValueDo(func(recovered interface{}) {
        fmt.Print(recovered) // "this was inevitable"
    })
    panic("this was inevitable")
}

func Cause

func Cause(err error) error

Wrapped

func ErrLog

func ErrLog(log logg.Logg, err error) logg.Logg

Log error and return Logger object

func Errorf

func Errorf(format string, args ...interface{}) error

Wrapped

func Errorv

func Errorv(message string, arg0 interface{}, args ...interface{}) error

Add contextual information to the end of the error string

func LogErrorThenDie

func LogErrorThenDie(log logg.Logg, err error)

Log error and return logger object

func New

func New(message string) error

Wrapped

func StackTrace

func StackTrace(err error) errorsOrig.StackTrace

func StackTraceString

func StackTraceString(err error) string

Get stacktrace from error object

func WasCausedBy

func WasCausedBy(err, matchErr error) bool

func WithMessage

func WithMessage(err error, message string) error

Wrapped

func WithMessagef

func WithMessagef(err error, format string, args ...interface{}) error

Wrapped

func WithMessagev

func WithMessagev(err error, message string, arg0 interface{}, args ...interface{}) error

Like Errorv(), but for WithMessage()

func WithStack

func WithStack(err error) error

Wrapped

func WithStacktrace

func WithStacktrace(log logg.Logg, err error) logg.Logg

func Wrap

func Wrap(err error, message string) error

Wrapped

func Wrapf

func Wrapf(err error, message string, args ...interface{}) error

Wrapped

func Wrapv

func Wrapv(err error, message string, arg0 interface{}, args ...interface{}) error

Like Errorv(), but for Wrap()

Types

type DoWithErrFunc

type DoWithErrFunc func(err error)

type DoWithLogAndMessageFunc

type DoWithLogAndMessageFunc func(err error, log logg.Logg, message string)

type DoWithLogFunc

type DoWithLogFunc func(err error, log logg.Logg)

type DoWithPanicFunc

type DoWithPanicFunc func(recovered interface{})

type PanicError

type PanicError struct {
	// contains filtered or unexported fields
}

func NewPanicError

func NewPanicError(recovered interface{}) (result PanicError)

func (PanicError) Error

func (pe PanicError) Error() string

Jump to

Keyboard shortcuts

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