Skip to content

Error wrapper for golang standard error, with Stacktrace/Fields, Logrus integration

License

Notifications You must be signed in to change notification settings

yyscamper/errors

Repository files navigation

errors

Build upon pkg/errors, with some customization for my customized yyscamper/logrus

Usage

import "github.com/yyscamper/errors"

var err error

//errors.New style
err = errors.New("some error")
if err != nil {
    ...
}

//fmt.Errorf style
err = errors.Errorf("some error, path=%s", "/home/me/foo.txt")
if err != nil {
    ...
}

//Wrap the existing error
path := "/home/me/foo.txt"
file, err = os.Open(path)
if err != nil {
    return errors.Wrap(err) //The callstack will be attached
}

Compatiblity

My error wrapper extends the golang error interface, so it can be casted into the golang error

Stacktrace

Stacktrace is automatically collected when the error is created

Fields

Designed for logrus, logrus can extract additional fileds from the error objects

errors.New("some error").WithField("path": "/home/me/foo.txt")
errors.New("some error").WithFields(errors.Fileds{
    "path": "/home/me/foo.txt",
    "mode": "readwrite",
    "time": time.Now(),
})
errors.New("some error").With("path": "/home/me/foo.txt", "time", time.Now())

Generator

Using the generator can automatically set a name for the error, it is useful for logrus module logging

errgen := errors.NewGenerator("testmodule")
err := errgen.New("some error")
err = errgen.Errorf("some error with path=%s", "/home/me/foo.txt")

Logrus Integration

I have a customized yyscamper/logrus, it extends the support for this error wrapper, so that the error stacktrace and all fields information can be automatically logged by logrus

import (
    log "github.com/yyscamper/logrus"
    "github.com/yyscamper/errors"
)

err := errors.New("some error").WithField("path", "/home/me/foo.txt")
log.WithError(err).Error("some happens")

About

Error wrapper for golang standard error, with Stacktrace/Fields, Logrus integration

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages