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

codes: replace %q to %d in error string when invalid code is an integer #7188

Merged

Conversation

purnesh42H
Copy link
Contributor

@purnesh42H purnesh42H commented May 3, 2024

7174(Minor 'invalid code' error output suggestion) raises an issue from implementation https://github.com/grpc/grpc-go/blob/b433b9467d87d70de277ee7e1139ef2ad900bfa4/codes/codes.go that line 238 fmt.Errorf("invalid code: %q", ci) will interpret the uint64 value as a Unicode code point and attempt to print the corresponding character. However, if invalid code is an integer and we already parsed ci as Uint, it is better to use %d instead of %q for the better output.

Before Change, code.UnmarshalJson("200") returns "invalid code: 'E'"

After change, code.UnmarshalJson("200") returns "invalid code: 200"

RELEASE NOTES: N/A

Copy link

linux-foundation-easycla bot commented May 3, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@purnesh42H purnesh42H changed the title Replace %q to %d for invalid code string formatting Replace %q to %d in error string when invalid code is an integer May 3, 2024
@purnesh42H purnesh42H force-pushed the improve-unmarshal-json-invalid-code-format branch from a0f7de3 to 23acaf4 Compare May 3, 2024 13:16
@purnesh42H purnesh42H added this to the 1.64 Release milestone May 5, 2024
Copy link
Contributor

@arjan-bal arjan-bal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@purnesh42H purnesh42H force-pushed the improve-unmarshal-json-invalid-code-format branch 4 times, most recently from 5e5c77d to 20c42a3 Compare May 6, 2024 11:50
codes/codes_test.go Outdated Show resolved Hide resolved
codes/codes_test.go Show resolved Hide resolved
codes/codes_test.go Outdated Show resolved Hide resolved
@easwars easwars assigned purnesh42H and unassigned easwars May 6, 2024
@purnesh42H purnesh42H force-pushed the improve-unmarshal-json-invalid-code-format branch 4 times, most recently from 21e4d71 to 0aacf4f Compare May 7, 2024 05:42
@purnesh42H purnesh42H force-pushed the improve-unmarshal-json-invalid-code-format branch from 0aacf4f to a6f25ed Compare May 7, 2024 05:57
@purnesh42H purnesh42H requested a review from easwars May 7, 2024 09:59
@aranjans
Copy link
Contributor

aranjans commented May 7, 2024

LGTM!

@easwars
Copy link
Contributor

easwars commented May 7, 2024

Also, for the PR title, we usually follow the following format:
package: <description starting with a lower case>

So, this would be:
codes: replace %q to %d in error string when invalid code is an integer

@purnesh42H purnesh42H changed the title Replace %q to %d in error string when invalid code is an integer codes: replace %q to %d in error string when invalid code is an integer May 8, 2024
@purnesh42H
Copy link
Contributor Author

Also, for the PR title, we usually follow the following format: package: <description starting with a lower case>

So, this would be: codes: replace %q to %d in error string when invalid code is an integer

Done

@purnesh42H purnesh42H assigned easwars and unassigned purnesh42H May 8, 2024
@easwars
Copy link
Contributor

easwars commented May 8, 2024

Also, while you are here, could you also please change the call to reflect.DeepEqual() with a call to cmp.Equal in TestJSONUnmarshal. The latter is preferred over the former. See: https://google.github.io/styleguide/go/decisions#equality-comparison-and-diffs. Thanks.

@easwars easwars assigned purnesh42H and unassigned easwars May 8, 2024
@purnesh42H
Copy link
Contributor Author

Also, while you are here, could you also please change the call to reflect.DeepEqual() with a call to cmp.Equal in TestJSONUnmarshal. The latter is preferred over the former. See: https://google.github.io/styleguide/go/decisions#equality-comparison-and-diffs. Thanks.

Done

@purnesh42H purnesh42H assigned easwars and unassigned purnesh42H May 9, 2024
Copy link
Contributor

@easwars easwars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo the one last minor nit.


var got Code
if err := got.UnmarshalJSON([]byte("200")); !strings.Contains(err.Error(), wantErr) {
t.Errorf("got.UnmarshalJSON(200) != %v; wantErr: %v", err, wantErr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message should have a = instead of a != because we are printing got before want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done

@easwars easwars assigned purnesh42H and unassigned easwars May 9, 2024
@purnesh42H purnesh42H assigned easwars and unassigned purnesh42H May 9, 2024
@dfawley dfawley merged commit 070d9c7 into grpc:master May 9, 2024
11 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants