GithubHelp home page GithubHelp logo

go-errors's Introduction

Coditory - Go Errors

GitHub release Go Reference Go Report Card Build Status Coverage

๐Ÿšง This library as under heavy development until release of version 1.x.x ๐Ÿšง

Wrapper for Go errors that prints error causes with theis stack traces.

  • Prints stacks traces from all of the causes
  • Shortens file paths and function names for readability
  • Supports and exports errors.Is, errors.As, errors.Unwrap

Getting started

Installation

Get the dependency with:

go get github.com/coditory/go-errors

and import it in the project:

import "github.com/coditory/go-errors"

The exported package is errors, basic usage:

import "github.com/coditory/go-errors"

func main() {
    err := foo()
    fmt.Println("Error with stack trace:")
    fmt.Println(errors.Format(err))

    stderr := fmt.Errorf("std error")
    fmt.Println("Go std error:")
    fmt.Println(errors.Format(stderr))
}

func foo() error {
    err := bar()
    return errors.Wrap(err, "foo failed")
}

func bar() error {
    return errors.New("bar failed")
}

Output:

Error with stack trace:
foo failed
    ./pkg/samples.go:34
        main.foo
    ./pkg/samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit
caused by: bar failed
    ./pkg/samples.go:38
        main.bar
    ./pkg/samples.go:33
        main.foo
    ./pkg/samples.go:10
        main.main
    go1.20.2/rc/runtime/proc.go:250
        runtime.main
    go1.20.2/rc/runtime/asm_amd64.s:1598
        runtime.goexit

Go std error:
std error

Verbosity levels

Errors can be formatted with different verbosity levels with:

fmt.Println(errors.Formatv(err), verbosity)

...or by changing the global verbosity level:
errors.Config.Verbosity = 5

The default verbosity level is 5.

Verbosity level samples generated with go run ./samples:

>>> Format: 0
foo failed

>>> Format: 1
foo failed
caused by: bar failed

>>> Format: 2
foo failed
	main.foo():19
	main.main():10
	runtime.main():250
	runtime.goexit():1598
caused by: bar failed
	main.bar():23
	main.foo():18
	main.main():10
	runtime.main():250
	runtime.goexit():1598

>>> Format: 3
foo failed
	./samples.go:19
	./samples.go:10
	go1.20.2/rc/runtime/proc.go:250
	go1.20.2/rc/runtime/asm_amd64.s:1598
caused by: bar failed
	./samples.go:23
	./samples.go:18
	./samples.go:10
	go1.20.2/rc/runtime/proc.go:250
	go1.20.2/rc/runtime/asm_amd64.s:1598

>>> Format: 4
foo failed
	./samples.go:19 foo()
	./samples.go:10 main()
	go1.20.2/rc/runtime/proc.go:250 main()
	go1.20.2/rc/runtime/asm_amd64.s:1598 goexit()
caused by: bar failed
	./samples.go:23 bar()
	./samples.go:18 foo()
	./samples.go:10 main()
	go1.20.2/rc/runtime/proc.go:250 main()
	go1.20.2/rc/runtime/asm_amd64.s:1598 goexit()

>>> Format: 5
foo failed
	main.foo()
		./samples.go:19
	main.main()
		./samples.go:10
	runtime.main()
		go1.20.2/rc/runtime/proc.go:250
	runtime.goexit()
		go1.20.2/rc/runtime/asm_amd64.s:1598
caused by: bar failed
	main.bar()
		./samples.go:23
	main.foo()
		./samples.go:18
	main.main()
		./samples.go:10
	runtime.main()
		go1.20.2/rc/runtime/proc.go:250
	runtime.goexit()
		go1.20.2/rc/runtime/asm_amd64.s:1598

>>> Format: 6
foo failed
	./samples.go:19
		foo()
	./samples.go:10
		main()
	go1.20.2/rc/runtime/proc.go:250
		main()
	go1.20.2/rc/runtime/asm_amd64.s:1598
		goexit()
caused by: bar failed
	./samples.go:23
		bar()
	./samples.go:18
		foo()
	./samples.go:10
		main()
	go1.20.2/rc/runtime/proc.go:250
		main()
	go1.20.2/rc/runtime/asm_amd64.s:1598
		goexit()

>>> Format: 7
foo failed
	/Users/mendlik/Development/go/go-errors/samples/samples.go:19
		main.foo()
	/Users/mendlik/Development/go/go-errors/samples/samples.go:10
		main.main()
	/Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/proc.go:250
		runtime.main()
	/Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit()
caused by: bar failed
	/Users/mendlik/Development/go/go-errors/samples/samples.go:23
		main.bar()
	/Users/mendlik/Development/go/go-errors/samples/samples.go:18
		main.foo()
	/Users/mendlik/Development/go/go-errors/samples/samples.go:10
		main.main()
	/Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/proc.go:250
		runtime.main()
	/Users/mendlik/.sdkvm/sdk/go/1.20.2/src/runtime/asm_amd64.s:1598
		runtime.goexit()

go-errors's People

Contributors

dependabot[bot] avatar pmendelski avatar manicar2093 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.