Same as fmt.Errorf but with stack trace.

Annotation with stack trace for go1.13

GoDoc Report Build Status

Go 1.13 contains support for error wrapping. Now you can add additional information to an error by wrapping it using the new %w verb at fmt.Errorf and examine such errors using errors.Is and errors.As. If you also want to save a stack trace of an error instead of fmt.Errorf use stack.Errorf which is compatible with errors.Is and errors.As and also gives the ability to get a stack trace of the error using stack.Trace function which will return []runtime.Frame.

  • Import.
import "github.com/romanyx/stack
  • Annotate error.
func example() error {
	if err := call(); err != nil {
		return stack.Errorf("call: %w", err)
	}

	return nil
}
  • Print original error.
stack.Origin(err)
  • Iterate through stack trace.
for _, frame := range stack.Trace(err) {
	fmt.Printf("%s:%d %s()\n", frame.File, frame.Line, frame.Function)
}
Owner
Roman Budnikov
Gopher. Twitter: @romanyx0
Roman Budnikov
Similar Resources

Golang errors with stack trace and source fragments.

Golang errors with stack trace and source fragments.

Golang Errors with Stack Trace and Source Fragments Tired of uninformative error output? Probably this will be more convenient: Example package main

Dec 17, 2022

A Go package providing errors with a stack trace Read-only

Errors with a stack trace A Go package providing errors with a stack trace. Features: Based of github.com/pkg/errors with similar API, addressing many

Sep 23, 2022

its the same idea as bruh-bot, but with golang, and add more bots

its the same idea as bruh-bot, but with golang, and add more bots

bruh-bot but more powerful! requirements python go you can used on mac and linux the idea its really simple, can make a lot of bots with the same task

Jul 7, 2021

go-test-trace is like go test but it also generates distributed traces.

go-test-trace is like go test but it also generates distributed traces.

go-test-trace go-test-trace is like go test but it also generates distributed traces. Generated traces are exported in OTLP to a OpenTelemetry collect

Jan 5, 2023

:art: Contextual fmt inspired by bootstrap color classes

Cfmt Contextual fmt It provides contextual formatting functions that have nearly identical usage of the fmt package. The ideas were borrowed from boot

Jan 7, 2023

A lightweight replacement for the standard fmt package, reduces binary size by roughly 400kb in a hello world

console This is a lightweight replacement for the fmt package, reduces the binary size by roughly 400kb in a hello world program. Please note: This pa

Nov 7, 2021

Translate your Go program into multiple languages with similar fmt.Sprintf format syntax.

Loafer-i18n Loafer-i18n is a Go package and a command that helps you translate Go programs into multiple languages. Supports pluralized strings with =

Dec 22, 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

Elastic Stack Docker + Sample Go AppElastic Stack Docker + Sample Go App

Elastic Stack Docker + Sample Go AppElastic Stack Docker + Sample Go App

📶 Elastic Stack Docker + Sample Go App Test Elastic Stack which includes Elasticsearch, Kibana, Filebeat and Metricbeat. It comes with a very simple

Jan 14, 2022

T# Programming Language. Something like Porth, Forth but written in Go. Stack-oriented programming language.

The T# Programming Language WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! Something like Forth a

Jun 29, 2022

Run the same Docker images in AWS Lambda and AWS ECS

Run the same Docker images in AWS Lambda and AWS ECS

serverlessish tl;dr Run the exact same image for websites in Lambda as you do in ECS, Kubernetes, etc. Just add this to your Dockerfile, listen on por

Dec 22, 2022

a persistent real-time key-value store, with the same redis protocol with powerful features

a persistent real-time key-value store, with the same redis protocol with powerful features

a fast NoSQL DB, that uses the same RESP protocol and capable to store terabytes of data, also it integrates with your mobile/web apps to add real-time features, soon you can use it as a document store cause it should become a multi-model db. Redix is used in production, you can use it in your apps with no worries.

Dec 25, 2022

Run your MapReduce workloads as a single binary on a single machine with multiple CPUs and high memory. Pricing of a lot of small machines vs heavy machines is the same on most cloud providers.

gomap Run your MapReduce workloads as a single binary on a single machine with multiple CPUs and high memory. Pricing of a lot of small machines vs he

Sep 16, 2022

NanoVGo NanoVGNanoVGo is pure golang implementation of NanoVG. The same author maintains the NanoGUI.go project mentioned above.

NanoVGo Pure golang implementation of NanoVG. NanoVG is a vector graphics engine inspired by HTML5 Canvas API. DEMO API Reference See GoDoc Porting Me

Dec 6, 2022

Handlebars for golang with the same features as handlebars.js 3.0

Handlebars for golang with the same features as handlebars.js 3.0. Hard fork of Raymond to modularize and keep up with handlebars development.

Nov 5, 2022

After approve this contract, you can use the contract to adventure with multiple characters at the same time

After approve this contract, you can use the contract to adventure with multiple characters at the same time

MultipleRarity 又又又更新了! MultipleRarity最新版:0x8ACcaa4b940eaFC41b33159027cDBDb4A567d442 注:角色冷却时间不统一时,可以不用管能不能冒险或升级,合约内部加了筛选,但消耗的gas增加了一点点,介意的可以使用常规修复版。 Mu

Nov 19, 2021

The server-pubsub is the main backend of DATAVOC project that manages all the other web-server modules of the same project such as the processor

server-pubsub The server-pubsub is the main backend of DATAVOC project that manages all the other web-server modules of the same project such as the p

Dec 3, 2021

Ditto is a CLI testing tool that helps you verify if multiple HTTP endpoints have the same outputs.

Ditto is a CLI testing tool that helps you verify if multiple HTTP endpoints have the same outputs.

Nov 24, 2021

Transfer files between machines in the same network

GoTrans Transfer files from computer A to computer B. The program have a sender and a receiver, if you choose a sender you'll need to provide the ip a

Nov 7, 2021
Comments
  • Cascading calls should not remove the first stack

    Cascading calls should not remove the first stack

    We loose the stack when the error is wrapped in cascade

    package main
    
    import (
    	"fmt"
    
    	"github.com/pkg/errors"
    	"github.com/romanyx/stack"
    )
    
    func a() error {
    	return errors.New("a")
    }
    func b() error {
    	return stack.Errorf("b :%v", a())
    }
    func c() error {
    	return stack.Errorf("c :%v", b())
    }
    func main() {
    	err := c()
    	for _, frame := range stack.Trace(err) {
    		fmt.Printf("%s:%d %s()\n", frame.File, frame.Line, frame.Function)
    	}
    }
    

    Result

    /tmp/t/t.go:17 main.c()
    /tmp/t/t.go:20 main.main()
    

    With pkg/errors it's the opposite, the stack is three time. I would prefer to have full stack like that

    /tmp/t/t.go:10 main.a()
    /tmp/t/t.go:13 main.b()
    /tmp/t/t.go:16 main.c()
    /tmp/t/t.go:19 main.main()
    
  • Use uintptr instead of runtime.Frame

    Use uintptr instead of runtime.Frame

    Before:

    BenchmarkErrorf/5-frames-4         	  158538	      7573 ns/op	    2992 B/op	      16 allocs/op
    BenchmarkErrorf/10-frames-4        	   89179	     13124 ns/op	    4072 B/op	      26 allocs/op
    BenchmarkErrorf/20-frames-4        	   44906	     26946 ns/op	    6232 B/op	      46 allocs/op
    BenchmarkErrorf/40-frames-4        	   18634	     78400 ns/op	   13754 B/op	      87 allocs/op
    

    After:

    BenchmarkErrorf/5-frames-4         	  937490	      1248 ns/op	     384 B/op	       6 allocs/op
    BenchmarkErrorf/10-frames-4        	  844726	      1510 ns/op	     384 B/op	       6 allocs/op
    BenchmarkErrorf/20-frames-4        	  659119	      1842 ns/op	     384 B/op	       6 allocs/op
    BenchmarkErrorf/40-frames-4        	  599695	      2035 ns/op	     384 B/op	       6 allocs/op
    
Related tags
Golang errors with stack trace and source fragments.
Golang errors with stack trace and source fragments.

Golang Errors with Stack Trace and Source Fragments Tired of uninformative error output? Probably this will be more convenient: Example package main

Dec 17, 2022
A Go package providing errors with a stack trace Read-only

Errors with a stack trace A Go package providing errors with a stack trace. Features: Based of github.com/pkg/errors with similar API, addressing many

Sep 23, 2022
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
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
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
Golang advanced error usage with stack tracing

UhOh Golang advanced error usage with stack tracing uhoh consists of 3 parts: Original error Description of error Stack trace File Function Line Usage

Mar 30, 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
Linter for Go's fmt.Errorf message

wrapmsg wrapmsg is Go code linter. this enforces fmt.Errorf's message when you wrap error. Example // OK ???? if err := pkg.Cause(); err != nil { re

Dec 27, 2022
SizedWaitGroup has the same role and close to the same API as the Golang sync.WaitGroup but it adds a limit on the amount of goroutines started concurrently.

SizedWaitGroup SizedWaitGroup has the same role and API as sync.WaitGroup but it adds a limit of the amount of goroutines started concurrently. SizedW

Jan 8, 2023
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