Skip to content

Instantly share code, notes, and snippets.

@tonyyang-svail
Last active January 11, 2019 21:17
Show Gist options
  • Save tonyyang-svail/a573de68c10897890acb778f3f170c49 to your computer and use it in GitHub Desktop.
Save tonyyang-svail/a573de68c10897890acb778f3f170c49 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/pkg/errors"
)
func bar() error {
return errors.Wrap(fmt.Errorf("barError"), "bar")
}
func foo() error {
return bar()
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
func main() {
err := foo()
if err, ok := err.(stackTracer); ok {
for _, f := range err.StackTrace() {
fmt.Printf("%+s:%d\n", f, f)
}
}
}
@tonyyang-svail
Copy link
Author

tonyyang-svail commented Jan 11, 2019

main.bar
	/tmp/goexample/main.go:10
main.foo
	/tmp/goexample/main.go:14
main.main
	/tmp/goexample/main.go:22
runtime.main
	/usr/local/go/src/runtime/proc.go:201
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:1333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment