package module
Version: v0.3.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2019 License: MIT Imports: 6 Imported by: 34


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:



package main

import (


func main() {
	if err := read(); err != nil {

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 ""
Create New Error
err := tracerr.New("some error")


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:


This will add source code:


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, 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()


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



Package tracerr makes error output more informative. It adds stack trace to error and can display error with source fragments.

Check example of output here



This section is empty.


View Source
var DefaultCap = 20

DefaultCap is a default cap for frames array. It can be changed to number of expected frames for purpose of performance optimisation.

View Source
var DefaultLinesAfter = 2

DefaultLinesAfter is number of source lines after traced line to display.

View Source
var DefaultLinesBefore = 3

DefaultLinesBefore is number of source lines before traced line to display.


func Print

func Print(err error)

Print prints error message with stack trace.

func PrintSource

func PrintSource(err error, nums

PrintSource prints error message with stack trace and source fragments.

By default 6 lines of source code will be printed, see DefaultLinesAfter and DefaultLinesBefore.

Pass a single number to specify a total number of source lines.

Pass two numbers to specify exactly how many lines should be shown before and after traced line.

func PrintSourceColor

func PrintSourceColor(err error, nums

PrintSourceColor prints error message with stack trace and source fragments, which are in color. Output rules are the same as in PrintSource.

func Sprint

func Sprint(err error) string

Sprint returns error output by the same rules as Print.

func SprintSource

func SprintSource(err error, nums string

SprintSource returns error output by the same rules as PrintSource.

func SprintSourceColor

func SprintSourceColor(err error, nums string

SprintSourceColor returns error output by the same rules as PrintSourceColor.

func Unwrap added in v0.2.0

func Unwrap(err error) error

Unwrap returns the original error.


type Error

type Error interface {
	Error() string
	StackTrace() []Frame
	Unwrap() error

Error is an error with stack trace.

func CustomError added in v0.3.0

func CustomError(err error, frames []Frame) Error

CustomError creates an error with provided frames.

func Errorf

func Errorf(message string, args ...interface{}) Error

Errorf creates new error with stacktrace and formatted message. Formatting works the same way as in fmt.Errorf.

func New

func New(message string) Error

New creates new error with stacktrace.

func Wrap

func Wrap(err error) Error

Wrap adds stacktrace to existing error.

type Frame

type Frame struct {
	// Func contains a function name.
	Func string
	// Line contains a line number.
	Line int
	// Path contains a file path.
	Path string

Frame is a single step in stack trace.

func StackTrace

func StackTrace(err error) []Frame

StackTrace returns stack trace of an error. It will be empty if err is not of type Error.

func (Frame) String

func (f Frame) String() string

String formats Frame to string.

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL