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

Return Close() errors instead of logging #66

Merged
merged 5 commits into from
Jan 31, 2018
Merged

Conversation

spenczar
Copy link
Contributor

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. If Close() 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.

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.
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()`)
Copy link

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?

Copy link

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.

Copy link
Contributor Author

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.

@rhysh
Copy link
Contributor

rhysh commented Jan 28, 2018

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 doProtobufRequest and doJSONRequest to use a named return and to replace their use of defer closebody(resp.Body) with deferring a closure like this:

defer func() {
  cerr := resp.Body.Close()
  if err == nil {
    err = clientError("failed to close response body", cerr)
  }
}()

@spenczar
Copy link
Contributor Author

@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.

@spenczar
Copy link
Contributor Author

@rhysh Thanks for the prompt, this is better with the defer structure.

@spenczar spenczar merged commit c0c4355 into master Jan 31, 2018
@spenczar spenczar deleted the return_close_errs branch January 31, 2018 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants