Golang errors with stack trace and source fragments.

Golang Errors with Stack Trace and Source Fragments

GoDoc Report Coverage Status Build Status

Tired of uninformative error output? Probably this will be more convenient:

Output

Example

package main

import (
	"io/ioutil"

	"github.com/ztrue/tracerr"
)

func main() {
	if err := read(); err != nil {
		tracerr.PrintSourceColor(err)
	}
}

func read() error {
	return readNonExistent()
}

func readNonExistent() error {
	_, err := ioutil.ReadFile("/tmp/non_existent_file")
	// Add stack trace to existing error, no matter if it's nil.
	return tracerr.Wrap(err)
}

Find more executable examples in examples dir.

How to Use

Import

import "github.com/ztrue/tracerr"

Create New Error

err := tracerr.New("some error")

Or:

err := tracerr.Errorf("some error %d", num)

Add Stack Trace to Existing Error

If err is nil then it still be nil with no stack trace added.

err = tracerr.Wrap(err)

Print Error and Stack Trace

Stack trace will be printed only if err is of type tracerr.Error, otherwise just error text will be shown.

This will print error message and stack trace if any:

tracerr.Print(err)

This will add source code:

tracerr.PrintSource(err)

It's able to set up number of lines of code to display for each frame, which is 6 by default:

tracerr.PrintSource(err, 9)

Or to set up number of lines before and after traced line:

tracerr.PrintSource(err, 5, 2)

The same, but with color, which is much more useful:

tracerr.PrintSourceColor(err)
tracerr.PrintSourceColor(err, 9)
tracerr.PrintSourceColor(err, 5, 2)

Save Output to Variable

It's also able to save output to variable instead of printing it, which works the same way:

text := tracerr.Sprint(err)
text := tracerr.SprintSource(err)
text := tracerr.SprintSource(err, 9)
text := tracerr.SprintSource(err, 5, 2)

Get Stack Trace

Stack trace will be empty if err is not an instance of tracerr.Error.

frames := tracerr.StackTrace(err)

Or if err is of type tracerr.Error:

frames := err.StackTrace()

Get Original Error

Unwrapped error will be nil if err is nil and will be the same error if err is not an instance of tracerr.Error.

err = tracerr.Unwrap(err)

Or if err is of type tracerr.Error:

err = err.Unwrap()

Performance

Stack trace causes a performance overhead, depending on a stack trace depth. This can be insignificant in a number of situations (such as HTTP request handling), however, avoid of adding a stack trace for really hot spots where a high number of errors created frequently, this can be inefficient.

Benchmarks done on a MacBook Pro 2015 with go 1.11.

Benchmarks for creating a new error with a stack trace of different depth:

BenchmarkNew/5    200000    5646 ns/op    976 B/op   4 allocs/op
BenchmarkNew/10   200000   11565 ns/op    976 B/op   4 allocs/op
BenchmarkNew/20    50000   25629 ns/op    976 B/op   4 allocs/op
BenchmarkNew/40    20000   65833 ns/op   2768 B/op   5 allocs/op
Similar Resources

Golang errors with stacktrace and context

merry Add context to errors, including automatic stack capture, cause chains, HTTP status code, user messages, and arbitrary values. The package is la

Nov 19, 2022

A Go (golang) package for representing a list of errors as a single error.

go-multierror go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error. This allows a fun

Jan 1, 2023

Hierarchical errors reporting done right in Golang

Hierarchical errors made right Hate seeing error: exit status 128 in the output of programs without actual explanation what is going wrong? Or, maybe,

Nov 9, 2021

Package semerr helps to work with errors in Golang.

Package semerr helps to work with errors in Golang.

semerr Package semerr helps to work with errors in Golang. Const error An error that can be defined as const. var errMutable error = errors.New("mutab

Oct 30, 2022

A Nostress Errors Package For Golang

A Nostress Errors Package For Golang

Nov 2, 2021

Go tool to wrap and fix errors with the new %w verb directive

Go tool to wrap and fix errors with the new %w verb directive

errwrap Wrap and fix Go errors with the new %w verb directive. This tool analyzes fmt.Errorf() calls and reports calls that contain a verb directive t

Nov 10, 2022

This structured Error package wraps errors with context and other info

RErr package This structured Error package wraps errors with context and other info. It can be used to enrich logging, for example with a structured l

Jan 21, 2022

The Emperor takes care of all errors personally

The Emperor takes care of all errors personally

The Emperor takes care of all errors personally. Go's philosophy encourages to gracefully handle errors whenever possible, but some times recovering f

Jan 9, 2023

A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.

A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.

Errors Errors package is a drop-in replacement of the built-in Go errors package with no external dependencies. It lets you create errors of 11 differ

Dec 6, 2022
Comments
  • matching api of https://github.com/pkg/errors

    matching api of https://github.com/pkg/errors

    would it be possible to add some missing functions like wrapf and errorf to match the interface of https://github.com/pkg/errors . then this can be a drop in replacement to that package.

    thanks

  • Supporting Go 1.13+ wrapped errors

    Supporting Go 1.13+ wrapped errors

    Go 1.13 supports wrapping errors in a standard way.

    tracerr doesn't print stack traces of wrapped errors, as it doesn't know how to look inside to see the wrapped error is a tracerr.Error.

    Changing the test in print.go from

            e, ok := err.(Error)
    	if !ok {
    		return err.Error()
    	}
    

    to

            var e Error
    	if !errors.As(err, &e) {
    		return err.Error()
    	}
    

    It's a fairly trivial change, but does mean that it won't compile with Go 1.12 or earlier, without some minor refactoring and build flags.

eris provides a better way to handle, trace, and log errors in Go 🎆

eris Package eris provides a better way to handle, trace, and log errors in Go. go get github.com/rotisserie/eris Why you'll want to switch to eris Us

Dec 29, 2022
Reduce debugging time while programming Go. Use static and stack-trace analysis to determine which func call causes the error.
Reduce debugging time while programming Go. Use static and stack-trace analysis to determine which func call causes the error.

Errlog: reduce debugging time while programming Introduction Use errlog to improve error logging and speed up debugging while you create amazing code

Nov 18, 2022
Drop-in replacement for the standard library errors package and github.com/pkg/errors

Emperror: Errors Drop-in replacement for the standard library errors package and github.com/pkg/errors. This is a single, lightweight library merging

Dec 20, 2022
Common juju errors and functions to annotate errors. Based on juju/errgo

errors import "github.com/juju/errors" The juju/errors provides an easy way to annotate errors without losing the original error context. The exporte

Dec 30, 2022
Linter for errors.Is and errors.As

erris erris is a program for checking that errors are compared or type asserted using go1.13 errors.Is and errors.As functions. Install go get -u gith

Nov 27, 2022
Same as fmt.Errorf but with stack trace.

Annotation with stack trace for go1.13 Go 1.13 contains support for error wrapping. Now you can add additional information to an error by wrapping it

Dec 25, 2020
An errors package optimized for the pkg/errors package
An errors package optimized for the pkg/errors package

errors An errors package optimized for the pkg/errors package Use Download and install go get github.com/dobyte/errors API // New Wrapping for errors.

Mar 2, 2022
Go package for errors with chained stack traces

errstack: Go errors with chained stack traces errstack is a Go package for creating errors with stack traces. It is heavily inspired by github.com/pkg

Nov 27, 2021
Package gostackparse parses goroutines stack traces as produced by panic() or debug.Stack() at ~300 MiB/s.

gostackparse Package gostackparse parses goroutines stack traces as produced by panic() or debug.Stack() at ~300 MiB/s. Parsing this data can be usefu

Dec 1, 2022
First attempt to trace a shell script with Datadog's go tracer
First attempt to trace a shell script with Datadog's go tracer

dd-trace-shell First attempt to trace a shell script with Datadog's go tracer. W

Dec 17, 2021