Code Monkey home page Code Monkey logo

goerr's Introduction

goerr

This package defines an error type that maintains a stack of nested errors and gives a human readable stack trace for logging.

Problem

In typical go error type, you don't have the stack trace of complete call chain. The only possibility is to log the stack trace at every function in the call chain. This will have multiple log entries made from single call and also the log are very cumbersome. Also the default stack trace contains lot many details that becomes difficult to read.

Principle

This package is to facilitate the well-known idiom

"throw error multiple times, but log once at the top most level"

For eg., if the call chain is controller -> service -> repository.

Then with goerr you return error from repository to service to controller and log the same in controller.

Output

If you return goerr in all methods and get the stack at top most level, it gives below nicely formatted, easily readable stack

controller failed [/Users/madhan.ganesh/src/github.com/madhanganesh/goerr/samplesrc/samples.go:11 (samplesrc.Controller)]
    service failed [/Users/madhan.ganesh/src/github.com/madhanganesh/goerr/samplesrc/samples.go:19 (samplesrc.Service)]
        error from database [/Users/madhan.ganesh/src/github.com/madhanganesh/goerr/samplesrc/samples.go:26 (samplesrc.Repository)]

Usage

Whenver you wanted to return an error just use

err := goerr.New(nil, "error in here")

if you have to nest the error, just pass it in New

err1 := goerr.New(err, "error in here")

Sample code that log in nested methods

func Controller() error{
	err := Service()
	if err != nil {
		return goerr.New(err, "controller failed")
	}
	return nil
}

func Service() error {
	err := Repository()
	if err != nil {
		return goerr.New(err, "service failed")
	}
	return err
}

func Repository() error {
	err := errors.New("error from database")
	return goerr.New(nil, err.Error())
}

Code to get the stack at the top most level

        err := samplesrc.Controller()
	if err != nil {
		t.Logf("error in controller: %s", goerr.Stack(err))
	}

Compatibility

  • goerr implements standard error interface, so can be assigned where ever error is used
  • goerr.Error() will give the error text of the top most error object
  • goerr.Stack(err) will give stack trace of call chain
  • goerr.Stack(err) can be called for error type as well, in which case it will just return Error()

Installation

go install github.com/madhanganesh/goerr

goerr's People

Contributors

madhanganesh 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.