GithubHelp home page GithubHelp logo

efimovalex / stackerr Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 137 KB

An golang error implementation with StatusCode and Function trace

Home Page: https://godoc.org/github.com/efimovalex/stackerr

License: MIT License

Go 100.00%
golang golang-library error-handling statuscode stacktrace trace go error error-log function

stackerr's Introduction

StackErr

CircleCI Go Report Card codecov GoDoc

An error implementation with StatusCode and Stacktrace

It implements the Golang error interface

You can use Status Code to better identify the answer you need to report to the client.

Makes debugging easier by logging the functions the error passes through and by adding the ability to log context on each function pass of the error, so that you can create a path of the error through your application.

Install

$ go get github.com/efimovalex/stackerr

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/efimovalex/stackerr"
)

func f1() *stackerr.Err {
	err := stackerr.NewWithStatusCode("message", http.StatusNotFound)
	return err.Stack()
}

func f2() *stackerr.Err {
	err := f1()
	return err.StackWithContext("context")
}

type t1 struct{}

func (t *t1) f3() *stackerr.Err {
	err := f2()
	return err.Stack()
}

func main() {
	ts := t1{}
	err := ts.f3()

	fmt.Println(err.Sprint())

	fmt.Println(err.Error())

	fmt.Println(err.StatusCode)

	if err.IsNotFound() {
		fmt.Println("Resource is not found")
	}

	err.Log()
}

Output:

Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:29 (main.main) 
-> /home/efi/workspace/stackerr/example/main.go:24 (main.(*t1).f3) 
-> /home/efi/workspace/stackerr/example/main.go:17 (main.f2) context
-> /home/efi/workspace/stackerr/example/main.go:11 (main.f1) 

message
404
Resource is not found

2021/10/26 17:58:11 Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:29 (main.main) 
-> /home/efi/workspace/stackerr/example/main.go:24 (main.(*t1).f3) 
-> /home/efi/workspace/stackerr/example/main.go:17 (main.f2) context
-> /home/efi/workspace/stackerr/example/main.go:11 (main.f1) 

2021/10/26 17:58:11 Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:41 (main.main) 

Authors

Created and maintained by

Efimov Alex - @efimovalex

License

MIT

stackerr's People

Contributors

efimovalex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kokizzu

stackerr's Issues

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.