This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 698
Help to print only one stack trace when multiple wrap #98
Comments
Can you please explain why you want to do this?
…On Sun, 8 Jan 2017, 23:23 Wilk ***@***.***> wrote:
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)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#98>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAcAzhVsQjLnQmzmV88aGaG3EZRj90Nks5rQNUxgaJpZM4LdrSU>
.
|
For example |
I cannot offer you a solution, only two pieces of advice.
1. I found that if my code is well factored then there was little use for
WithMessage as the name of the function was near identical to the message I
would have added. I suggest you look at the structure of you code and see
if the stack trace naturally avoids the need to add a message at every
step. Eg
func ReadFile(f string) error {
if err := ... ; err != nil {
return errors.WithMessage(err, "ReadFile failed) // usually
redundant
}
...
}
2. There is an open issue to added a stack trace if one does not exist, but
nobody has proposed an algorithm to do this, and I'm pretty sure if we did
implement this, there would be a request to skip this behaviour.
My recommendation is
If the error comes from outside you code, wrap it
If the error is being propogated from within your code, use WithMessage if
it adds real information,
Otherwise, return the error untouched.
…On Mon, 9 Jan 2017, 07:25 Wilk ***@***.***> wrote:
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 ?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#98 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAcA9y0XdIwjbHA7GbZmSrDZkss3gkBks5rQUYogaJpZM4LdrSU>
.
|
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.
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 ?
It print
I would like to print only the stack of three.
Thanks (and sorry for my english)
The text was updated successfully, but these errors were encountered: