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
remove error-inject
and fix error handling
#1409
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45e6659
remove `error-inject` and fix error handling
tinovyatkin 0eef9ff
Merge branch 'master' into remove-error-inject
tinovyatkin 39e5aa9
Merge branch 'master' into remove-error-inject
tinovyatkin 3921003
add failing test
tinovyatkin f4de618
Merge branch 'master' into remove-error-inject
tinovyatkin e85c77a
Merge branch 'master' into remove-error-inject
tinovyatkin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
const response = require('../helpers/context').response; | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const Stream = require('stream'); | ||
|
||
describe('res.body=', () => { | ||
describe('when Content-Type is set', () => { | ||
|
@@ -108,6 +109,16 @@ describe('res.body=', () => { | |
res.body = fs.createReadStream('LICENSE'); | ||
assert.equal('application/octet-stream', res.header['content-type']); | ||
}); | ||
|
||
it('should add error handler to the stream, but only once', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case clearly shows where the problem is, LGTM |
||
const res = response(); | ||
const body = new Stream.PassThrough(); | ||
assert.strictEqual(body.listenerCount('error'), 0); | ||
res.body = body; | ||
assert.strictEqual(body.listenerCount('error'), 1); | ||
res.body = body; | ||
assert.strictEqual(body.listenerCount('error'), 1); | ||
}); | ||
}); | ||
|
||
describe('when a buffer is given', () => { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The two implementations seem to be not equal. We should only add
error
event listener when the stream has no other error event listener, which aim to avoid uncaught exception.Also IMO koa's code philosophy is to keep code simple, so we don't treat 'one-liner module' as a bad thing.
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.
In current implementation the check in the
error-inject
does nothing (as two inline created functions never the same) and just always add.on('error')
handler. Do you mean that was desired behaviour?I'm proposing to remove
error-inject
here not because it's one line, but because even you (module author) forgot that you are breaking it functionality while refactored to inline function. It just does nothing in current version.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.
i don't understand what you mean, can you explain in a test?
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.
@jonathanong Sorry for my English. I've added a failing test to this PR, it's pretty simple:
This fails in Koa
master
and passing after merging this PR.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.
seems like a developer error
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.
It is, but the most probable explanation here is that by hiding the actual logic in a one-line module even the author of the module forgot the logic on a next refactoring and broke the functionality. That's what this PR is all about - fix the error and bring the logic inside to prevent such a problem in the future.