Coder Social home page Coder Social logo

serr's Introduction

SErr package

The Structured Error package can enrich an error with attributes at each stack frame while the error is being bubbled up to the caller without concern for logging at each frame. The SErr can then be logged with a structured logger like github.com/rohanthewiz/logger or printed with it's own string functions.

Usage

(See the included tests for more examples)

package main

import (
	"errors"

	"github.com/rohanthewiz/logger
	"github.com/rohanthewiz/serr
)

func main() {
	// Given an error
	err := errors.New("some error has occurred")

	// We can wrap the error with a message
	errWrapped := serr.Wrap(err, "Error occurred when trying to do things")

	// We can printout errors and attributes in a nice format
	fmt.Println(serr.StringFromErr(errWrapped))
	// ==> some error has occurred => location->logtest/main.go:16; function->main.main; msg->Error occurred when trying to do things

	// A structured error aware logger like github.com/rohanthewiz/logger can output all attributes
	logger.LogErr(errWrapped, "An Error occurred")
	// => ERRO[0000] some error has occurred	error="some error has occurred" fields.msg="An Error occurred - Error occurred when trying to do things"
	// function=main.main location="logtest/main.go:16"

	// We can wrap an error with some attributes
	err3 := serr.Wrap(err, "cats", "okay", "dogs", "hmm")

	logger.LogErr(err3, "Animals are cool")
	// => ERRO[0000] some error has occurred  cats=okay dogs=hmm error="some error has occurred" fields.msg="Animals are cool"
	// function=main.main location="logtest/main.go:27"
}

serr's People

Contributors

rohanthewiz avatar rohanallison avatar

Stargazers

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