Skip to content
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

Merged
merged 4 commits into from
Jun 20, 2023
Merged

Conversation

drevell
Copy link
Contributor

@drevell drevell commented Jun 15, 2023

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

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
@drevell drevell requested a review from a team as a code owner June 15, 2023 21:06
shankiyani
shankiyani previously approved these changes Jun 15, 2023
kevya-google
kevya-google previously approved these changes Jun 15, 2023
testutil/error.go Outdated Show resolved Hide resolved
Copy link
Contributor

@sethvargo sethvargo left a 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

@drevell
Copy link
Contributor Author

drevell commented Jun 15, 2023

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

@drevell drevell dismissed stale reviews from kevya-google and shankiyani via 3f45f80 June 15, 2023 22:43
Copy link
Contributor

@sethvargo sethvargo left a 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)
}

testutil/error.go Outdated Show resolved Hide resolved
@drevell
Copy link
Contributor Author

drevell commented Jun 20, 2023

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 don't think that would be as good. Consider the case where we're just matching a short substring of a large error, like DiffErrString(aGiganticErr, "does not exist"). In that case, you'd probably rather see a message like got error %q but want an error containing %q. A diff would be more confusing to read when comparing small thing to a large thing and they don 't have any similarity.. The check for "both got and want are large" is a decent heuristic for when a diff would be helpful and readable.

@drevell drevell merged commit 757fe7f into main Jun 20, 2023
@drevell drevell deleted the origin/includediff branch June 20, 2023 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants