-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sometimes, add a -/+ diff to DiffErrString output #141
Conversation
This helps users see at a glance where two errors are different, when errors are large. To avoid cluttering the output with -got/+want output when unneeded, we guard this logic to only run when either: - the message sizes are both greater than 20 bytes OR - the messages are both multiple lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we would be better off just using cmp on the string? https://pkg.go.dev/github.com/google/go-cmp/cmp
I can't quite understand what the suggestion is here. If the suggestion is to use that library to generate the diff, that's what this code already does. If the suggestion is to remove DiffErrString() in favor of using cmp.Diff directly, that seems like a step backward. DiffErrString is really clean to use because it handles
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we would be better off just using cmp on the string? https://pkg.go.dev/github.com/google/go-cmp/cmp
I can't quite understand what the suggestion is here.
If the suggestion is to use that library to generate the diff, that's what this code already does.
If the suggestion is to remove DiffErrString() in favor of using cmp.Diff directly, that seems like a step backward. DiffErrString is really clean to use because it handles
- empty/nil
- substring contains
- calling error.Error() for you
Sorry I wasn't super clear. I wasn't suggesting that we replace this with cmp
; I was suggesting that we ask cmp
to do the diff (after we've done nil-checks). So it'd look like:
func DiffErrString(got error, want string) string {
if want == "" {
if got == nil {
return ""
}
return fmt.Sprintf("got error %q but want <nil>", got.Error())
}
if got == nil {
return fmt.Sprintf("got error <nil> but want an error containing %q", want)
}
return cmp.Diff(got.Error(), want)
}
I don't think that would be as good. Consider the case where we're just matching a short substring of a large error, like |
This helps users see at a glance where two errors are different, when errors are large.
To avoid cluttering the output with -got/+want output when unneeded, we guard this logic to only run when either: