-
Notifications
You must be signed in to change notification settings - Fork 328
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
Return Close() errors instead of logging #66
Conversation
It's best not to call the standard library's logger directly without any configuration, so this changes clients to return errors encountered when calling resp.Body.Close() instead of logging them. If the client encounters a different error, any error from Close() will be ignored. If Close() returns an error, we'll call it a second time due to the defer, but this should be harmless.
protoc-gen-twirp/generator.go
Outdated
t.P(` if err := body.Close(); err != nil {`) | ||
t.P(` `, t.pkgs["log"], `.Printf("error closing body: %q", err)`) | ||
t.P(` }`) | ||
t.P(` err := body.Close()`) |
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.
How about _ = body.Close()
, possibly with the comment from the next line?
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.
Or, sorry, maybe it's done this way to get around errcheck -blank
? I've used _ = body.Close()
or similar before to appease errcheck but maybe -blank
is a mode I'm not familiar with.
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.
Correct, this is done to dodge errcheck -blank
, which is "errcheck, and I don't want any funny business like assigning to _
".
Which is exactly what we're doing, of course. But I think this is simpler than telling an opinionated user to change their linter setup to not invoke errcheck -blank
.
For clients, this helper doesn't seem to pay its way. It's used in two places in the client code, and the needs of the server code are now different. Instead of modifying the helper, how about phasing it out (first for client code, later for server code)? I propose changing
|
@rhysh I'm basically just paranoid of messing up the whole "named return + defer override" trick. I've seen it go awry too many times in too-subtle ways. Maybe we can do it here though if we do really really really careful code review and tests :) I'll take a crack at it and see how it feels. |
@rhysh Thanks for the prompt, this is better with the |
Issue #, if available:
This is one half of #4 . The other half is to fix servers to never log.
Description of changes:
It's best not to call the standard library's logger default logger, so this changes clients to return errors encountered when calling
resp.Body.Close()
instead of logging them.If the client encounters a different error, any error from
Close()
will be ignored. IfClose()
returns an error, we'll call it a second time due to the defer, but this should be harmless.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.