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

feat: ignore set/remove header/status when header sent #1137

Merged
merged 2 commits into from
Feb 11, 2018

Conversation

dead-horse
Copy link
Member

from #1078

we should not throw headers sent error to make ctx.responed and ctx.flushHeaders more safety.

@dead-horse dead-horse changed the title feat: ignore set header/status when header sent feat: ignore set/remove header/status when header sent Feb 7, 2018
@@ -133,8 +134,6 @@ module.exports = {
const original = this._body;
this._body = val;

if (this.res.headersSent) return;
Copy link
Member Author

Choose a reason for hiding this comment

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

we shouldn't return here because there are some specific logic for streams below.

@codecov
Copy link

codecov bot commented Feb 7, 2018

Codecov Report

Merging #1137 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1137      +/-   ##
==========================================
+ Coverage   99.73%   99.73%   +<.01%     
==========================================
  Files           5        5              
  Lines         372      374       +2     
==========================================
+ Hits          371      373       +2     
  Misses          1        1
Impacted Files Coverage Δ
lib/response.js 100% <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 0923ef6...e3f56a2. Read the comment docs.

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

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

LGTM

@fengmk2
Copy link
Member

fengmk2 commented Feb 7, 2018

Should patch this feature into koa v1

@dead-horse
Copy link
Member Author

I'd like to consider it as a minor version change because it only avoid throw exception when set header after header sent, which bothered developers using 1.x and 2.x. And it is harmless if you don't break koa's response handling by ctx.flushHeader or ctx.responed.

assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
assert(!this.res.headersSent, 'headers have already been sent');
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand my opinion wasn't asked for, but I'm considering if this could be a breaking change.
Is there any reason a user would rely on something like:

try {
  ctx.status = 201
} catch (err) {
  if (err.message.match(/headers have already been sent/) {
    // ...
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it is hard to make this decision, but even in this situation, you can do nothing but log this error because the http header has already sent.

@dead-horse
Copy link
Member Author

Any suggestions? Or I'll merge as a minor change and port it to 1.x.

@dead-horse dead-horse merged commit 3c23aa5 into master Feb 11, 2018
@dead-horse dead-horse deleted the ignore-header-sent branch February 11, 2018 08:25
@noinkling
Copy link
Contributor

noinkling commented Feb 11, 2018

This error can be a very useful indicator that there's a flaw in my app's logic somewhere, therefore I'm a bit wary of this change because now it will be harder to detect those sorts of bugs. Is there any way we could have the best of both worlds?

@dead-horse
Copy link
Member Author

@noinkling if you don't use ctx.responed or ctx.flushHeader, there is nothing to worry about.

if you do use these methods, you should take care of them and avoid to get into this situation.

@noinkling
Copy link
Contributor

noinkling commented Feb 11, 2018

In my case it's libraries that are doing it (e.g. wrapped Express middlewares), where I shouldn't be trying to mess with the response later in the chain. But I see your point that there should be no difference under normal usage, so I withdraw my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants