Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Help to print only one stack trace when multiple wrap #98

Closed
flibustenet opened this issue Jan 8, 2017 · 4 comments
Closed

Help to print only one stack trace when multiple wrap #98

flibustenet opened this issue Jan 8, 2017 · 4 comments

Comments

@flibustenet
Copy link

When I return an error I wrap because i'm never sure if it's already wrapped or not.
Then when i print the error with "%+v" it print all the stacks.
How can I do to only print the last stack but also all the messages ?

package main

import (
	"fmt"

	"github.com/pkg/errors"
)

func three() error {
	return errors.New("three")
}
func two() error {
	return errors.Wrap(three(), "two")
}
func one() error {
	return errors.Wrap(two(), "one")
}
func main() {
	err := one()
	fmt.Printf("%+v", err)
}

It print

three
main.three
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:10
main.two
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:13
main.one
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:16
main.main
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:19
runtime.main
	/usr/local/go/src/runtime/proc.go:183
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:2086
two
main.two
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:13
main.one
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:16
main.main
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:19
runtime.main
	/usr/local/go/src/runtime/proc.go:183
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:2086
one
main.one
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:16
main.main
	/home/wilk/projets/logics/medicoop/src/medicoop/t/t.go:19
runtime.main
	/usr/local/go/src/runtime/proc.go:183
runtime.goexit
	/usr/local/go/src/runtime/asm_amd64.s:2086

I would like to print only the stack of three.

Thanks (and sorry for my english)

@davecheney
Copy link
Member

davecheney commented Jan 8, 2017 via email

@flibustenet
Copy link
Author

For example one is my handler, two is my lib three is where i open a file.
I have an io error on three, i wrap the error "file not found" and return it.
Then in my lib i wrap this error, add a message "i could not get my data" and return it to the handler
On my handler i wrap this error and return it to the recovery middleware where i want to print the stack of the error.
I could not wrap the error in two and one and just add a message with WithMessage, but i don't know if the original error (in three) was wrapped (if i forget) so i wrap it isn't it ?

@davecheney
Copy link
Member

davecheney commented Jan 8, 2017 via email

@flibustenet
Copy link
Author

Thanks for your advice, i see now that it's a duplicate of the #75

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants