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

Fix for #1334 #1335

Merged
merged 2 commits into from
May 24, 2019
Merged

Fix for #1334 #1335

merged 2 commits into from
May 24, 2019

Conversation

noamt
Copy link
Contributor

@noamt noamt commented May 9, 2019

Commit the response code only when actually starting to write the payload

noamt added 2 commits May 9, 2019 11:19
…ode #1334 :

`response.Write` automatically sets status to `200` if a response code wasn't committed yet. This is convenient, but it ignores the fact that `response.Status` is a public field that may be set separately/before `response.Write` has been called
A `response.Status` is by default `0`, or `200` if the response was reset, so `response.Write` should fallback to `200` only if a code wasn't set yet.
…ode #1334 :

Writing the response code before encoding the payload is prone to error.
If JSON encoding fails, the response code is already committed, the server is able to only modify the response body to reflect the error and the user receives an awkward response where the status is successful but the body reports an error.
Instead - set the desired code on `c.response.Status`. If writing eventually takes place, the desired code is committed. If an error occurs, the server can still change the response.
@codecov
Copy link

codecov bot commented May 9, 2019

Codecov Report

Merging #1335 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1335      +/-   ##
==========================================
+ Coverage   84.34%   84.35%   +0.01%     
==========================================
  Files          27       27              
  Lines        2012     2014       +2     
==========================================
+ Hits         1697     1699       +2     
  Misses        205      205              
  Partials      110      110
Impacted Files Coverage Δ
response.go 71.05% <100%> (+1.6%) ⬆️
context.go 90.71% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d2c33a...f45968e. Read the comment docs.

@vishr
Copy link
Member

vishr commented May 17, 2019

@noamt Sorry for the delay. Few points:

  • Lets cover all type of response e.g. XML etc.
  • I think we should be ok without checking for status == 0 in WriteHeader function
  • You don't have to write test for all type of responses, the one you have written is good. Just remove JSON word from the test function name so it is generic.

@noamt
Copy link
Contributor Author

noamt commented May 23, 2019

@vishr I don't see any use of applying this to XML or any other of the methods, as this affects only json.
In XML for example, the XML header is written before the body has been encoded:

echo/context.go

Lines 484 to 487 in e2671fe

if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return
}
return enc.Encode(i)

XML encoding errors can't be reflected to the user in the current situation.

@vishr vishr merged commit fbb7286 into labstack:master May 24, 2019
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.

2 participants