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: handle some edge cases with errors #247

Closed
wants to merge 4 commits into from

Conversation

ronag
Copy link
Contributor

@ronag ronag commented Apr 13, 2019

You can't write headers if headers are already sent... which is the case if e.g. the read stream fails (emit error) AFTER initial byte.

I'm not sure checking writable is enough and also if headers are sent we still need to destroy the response.

@ronag ronag changed the title fix: handle some edge cases for error cases fix: handle some edge cases with errors Apr 13, 2019
@ronag ronag force-pushed the fix-edge-cases branch 2 times, most recently from 715b4f8 to 458b2aa Compare April 13, 2019 11:26
@codecov-io
Copy link

codecov-io commented Apr 13, 2019

Codecov Report

Merging #247 into master will increase coverage by 0.26%.
The diff coverage is 41.17%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #247      +/-   ##
==========================================
+ Coverage   75.31%   75.58%   +0.26%     
==========================================
  Files          11       11              
  Lines         555      557       +2     
  Branches      126      126              
==========================================
+ Hits          418      421       +3     
- Misses         48       50       +2     
+ Partials       89       86       -3
Impacted Files Coverage Δ
lib/ecstatic/show-dir/index.js 81.08% <0%> (+6.08%) ⬆️
lib/ecstatic.js 72.92% <0%> (ø) ⬆️
lib/ecstatic/status-handlers.js 64.7% <58.33%> (-5.07%) ⬇️

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 d0c3e94...8457903. Read the comment docs.

Copy link
Owner

@jfhbrook jfhbrook left a comment

Choose a reason for hiding this comment

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

Nice find! I left a comment about next(), let's talk about it!

@@ -2,58 +2,58 @@

const he = require('he');

function wrap(statusCode, fn) {
return (res, next, opts) => {
if (typeof next === 'function') {
Copy link
Owner

@jfhbrook jfhbrook Apr 13, 2019

Choose a reason for hiding this comment

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

This is inconsistent with the current behavior, right?

By my research, it looks like current behavior is to only check handleError in 404 cases and that it's not wired up literally anywhere else, and that prior to this change "next" was ignored, even though it was being passed in. 🤔 Not sure what the intent was there.

So there are a couple of things that would be good to see, if you're game:

  • Making sure that the next() calls are consistent with handleError usage
  • A documentation update to make this behavior more clear - shouldn't be a big deal, just describing that handleError: false will fall-through for whatever set of status codes
  • Some tests that check that next is called properly depending on the state of handleError. I realize that the tests are kind of a shitshow. If this is a relatively low lift 🤞 great! If not, maybe not a big deal.

Copy link
Contributor Author

@ronag ronag Apr 13, 2019

Choose a reason for hiding this comment

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

Rather than making things complicated... I think I've fixed it so that it is consistent with the current behavior. Please see latest revision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to admit I don't understand the current behavior... but let's fix the current behavior first and then consider changing it...

Copy link
Owner

Choose a reason for hiding this comment

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

Honestly, I don't either. This is a project I wrote a long time ago and I haven't held the whole thing in my head for a while. Honestly there are a lot of things I would do different in a rewrite, but the test harness is awkward enough that I think a refactor that size would be challenging.

Anyway, ignoring next(), or removing its use from the call sites is fine by me, though one of us should flag this as an issue.

Copy link
Owner

Choose a reason for hiding this comment

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

Written up here: #251 feel free to add notes if you have any.

@ronag ronag force-pushed the fix-edge-cases branch 2 times, most recently from 3477f55 to 351ee1d Compare April 14, 2019 11:58
@ronag
Copy link
Contributor Author

ronag commented Apr 14, 2019

Actually I think I figured it out pretty well with minimal changes.

if next is thruthy, act like a middleware.
if next is falsy, act like a server.

Consistent behaviour only required a few changes which I believe to be rather appropriate.

@jfhbrook
Copy link
Owner

Merged manually and released as 4.1.1. Thanks!

@jfhbrook jfhbrook closed this Apr 20, 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.

None yet

3 participants