Code Monkey home page Code Monkey logo

kverrors's Introduction

kverrors Build Status GoDoc

pkg kverrors is a small wrapper to pkg/errors for supporting structured errors for logging.


why structured

From go-kit/log

Structured logging is, basically, conceding to the reality that logs are data, and warrant some level of schematic rigor. Using a stricter, key/value-oriented message format for our logs, containing contextual and semantic information, makes it much easier to get insight into the operational activity of the systems we build
In short, A structured log message is not only a simple plain text message for a human to read, but a collection of key-values attributes which can be processed and analysed.

Errors most common method is logging, and treating errors as structured as well may give us better insight about our errors. This package is an experiment for using structured errors for logging.

Wrapping an error:

Wrapping an error is done using Wrap function:

func Wrap(err error, msg string, keyvals ...interface{}) error

For example:

if err != nil {
    return kverrors.Wrap(err, "somePkg.someFunc",
        "someKey", "some value",
    )
}

KeyVals

The function KeyVals returns the aggregated keyvals across the error chain. the error chain considered to be stopped when the error doesn't unwraps to an inner error.

KeyVals(err error) []interface{}

The function KeyValsMap returns the keyvals as map of key-value pairs across the error chain. This function have some constraints:

  • If the keyvals length across the error chain isn't even, it adds a "missing" key.
  • If a key isn't a string, it ignores it and put a placeholder.
KeyValsMap(err error) map[string]interface{}

Stack Trace

kverrors depends on pkg/errors for generating the stack trace. All errors returned with pkg kverrors implements the stacker interfface{}

type stacker interface {
    StackTrace() []pkgerrors.StackTrace
}

Unwrapping an error

Errors returned in kverrors can unwrap to the inner error by implementing

Unwrap() error

Unwrap to the original error

Package kverrors exports function

UnwrapAll()

USing this function you can get the original error in the error chain. This function iteratively goes over the error chain using the method Unwrap, and returns the first error which doesn't implement this interface.

Support of Go 1.13 and pkg/errors

Implementing Unwrap makes it possible to use Go's errors package Is and As method.

Also, the error chain may contain any error which implement Unwrap, meaning you can use kverrors.Wrap together with pkgerrors.Wrap and fmt.Errorf("%w", err)

kverrors's People

Contributors

orenrosen avatar

Stargazers

Steve Coffman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.