Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export class Response extends Macroable implements ResponseContract {
* Listen for errors on the stream and properly destroy
* stream
*/
body.on('error', (error) => {
body.on('error', (error: NodeJS.ErrnoException) => {
/* istanbul ignore if */
if (finished) {
return
Expand All @@ -283,7 +283,7 @@ export class Response extends Macroable implements ResponseContract {
} else {
this._end(
error.code === 'ENOENT' ? 'File not found' : 'Cannot process file',
error.status || 500,
error.code === 'ENOENT' ? 404 : 500,
)
resolve()
}
Expand Down
13 changes: 13 additions & 0 deletions test/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,19 @@ test.group('Response', (group) => {
assert.equal(text, 'hello world')
})

test('raise error when we try to stream a non-existing file', async (assert) => {
const server = createServer((req, res) => {
const config = fakeConfig()
const response = new Response(req, res, config)
response.stream(createReadStream(join(fs.basePath, 'i-dont-exist.txt')))
response.finish()
})

const { text, status } = await supertest(server).get('/')
assert.equal(status, 404)
assert.equal(text, 'File not found')
})

test('raise error when input is not a stream', async (assert) => {
assert.plan(1)

Expand Down