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 697
WithMessage appends message after associated stack traces instead of before #102
Comments
I can confirm the issue. I also expected the full error message at the top. |
Sorry, that's how it's designed, the inner most error goes on the top.
…On Wed, 14 Jun 2017, 18:06 benma ***@***.***> wrote:
I can confirm the issue. I also expected the full error message at the top.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#102 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAcA74NBfxiMYyvC0UMmEO6GKFM5Y9vks5sD5SFgaJpZM4L4z5i>
.
|
Ok, thanks. |
For reference, if anyone finds it useful, this is the function I use to format the stack trace. It puts the full error message on top and removes the quadratic blowup of the stack trace when there are many wraps (see #75 (comment)): func ErrFormat(err error) string {
if err == nil {
return ""
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
cause := errors.Cause(err)
if stackTrace, ok := cause.(stackTracer); ok {
buf := bytes.Buffer{}
for _, frame := range stackTrace.StackTrace() {
buf.WriteString(fmt.Sprintf("\n%+v", frame))
}
return err.Error() + buf.String()
}
return err.Error()
} |
This is AWESOME!
It's precisely what I wanted for users of this package.
There is no way that I could choose one format that would fit everyone's
needs, so I wanted to provide the raw materials so you could define your
own format.
…On Wed, Jun 14, 2017 at 7:12 PM, benma ***@***.***> wrote:
For reference, if anyone finds it useful, this is the function I use to
format the stack trace. It puts the full error message on top and removes
the quadratic blowup of the stack trace when there are many wraps (see #75
(comment) <#75 (comment)>
):
func ErrFormat(err error) string {
if err == nil {
return ""
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
cause := errors.Cause(err)
if stackTrace, ok := cause.(stackTracer); ok {
buf := bytes.Buffer{}
for _, frame := range stackTrace.StackTrace() {
buf.WriteString(fmt.Sprintf("%+v\n", frame))
}
return err.Error() + "\n" + buf.String()
}
return err.Error()
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#102 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAcA0-M8PkycAQPLYHhDkjECqIYmn8Fks5sD6QHgaJpZM4L4z5i>
.
|
I am glad you did ;) Maybe it would be worth it to include a few different formatters in the library so new users don't get any surprises and don't need to spend time doing their own? So far we've identified at least one different one where multiple users have been surprised before, and likely will again, so we could save their time dealing with this. |
I don't want to add helper formatter functions because they will begat more
and more increasingly undifferentiated formatters.
However, I'm happy to take PRs that add examples of how to write your own
formatter.
…On Wed, Jun 14, 2017 at 8:50 PM, benma ***@***.***> wrote:
I am glad you did ;)
Maybe it would be worth it to include a few different formatters in the
library so new users don't have get any surprises and don't need to spend
time doing their own? So far we've identified at least one different one
where multiple users have been surprised before, and likely will again, so
we could save their time dealing with this.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#102 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAcA0DZMyuz7CAVAQnrqFn1W4c6rgnhks5sD7sPgaJpZM4L4z5i>
.
|
I understand. Thanks! |
Here's a formatter I just finished... func printError(w io.Writer, err error) {
// Messages come back from deepest to shallowest; reverse the order for printing.
msgs, stack := deconstruct(err)
for i := range msgs {
if i > 0 {
fmt.Fprint(w, "\n...caused by ")
}
fmt.Fprint(w, msgs[len(msgs)-i-1])
}
if stack != nil {
fmt.Fprintf(w, "%+v\n", stack)
}
}
func deconstruct(err error) ([]string, errors.StackTrace) {
if err == nil {
return nil, nil
}
myMsg := err.Error()
var messages []string
var stack errors.StackTrace
type causer interface {
Cause() error
}
if causer, ok := err.(causer); ok {
messages, stack = deconstruct(causer.Cause())
if len(messages) > 0 {
pos := strings.Index(myMsg, ": "+messages[len(messages)-1])
if pos >= 0 {
myMsg = myMsg[:pos]
}
}
}
messages = append(messages, myMsg)
if stack == nil {
type stackTracer interface {
StackTrace() errors.StackTrace
}
if tracer, ok := err.(stackTracer); ok {
stack = tracer.StackTrace()
}
}
return messages, stack
} Example output:
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
After amending an error using errors.WithMessage, printing the error with
%+v
prints the extra information after the stack trace instead of with the original message. For example:This prints:
As mentioned in #75, it seems like it would be more helpful to include the additional information with original error message. When using
%v
with the same example, it prints messages in this way now:The text was updated successfully, but these errors were encountered: