From 919107e78d5e421f8fd414302d4d5d9d23da1da7 Mon Sep 17 00:00:00 2001 From: Romain Lanz Date: Wed, 23 Oct 2019 15:10:34 +0200 Subject: [PATCH] fix(Response): correctly send the response when file doesn't exist while streaming it --- src/Response/index.ts | 4 ++-- test/response.spec.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Response/index.ts b/src/Response/index.ts index f8746ed..17386da 100644 --- a/src/Response/index.ts +++ b/src/Response/index.ts @@ -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 @@ -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() } diff --git a/test/response.spec.ts b/test/response.spec.ts index da65b59..8488f09 100644 --- a/test/response.spec.ts +++ b/test/response.spec.ts @@ -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)