Skip to content

Commit

Permalink
Correctly forward stream errors (#424)
Browse files Browse the repository at this point in the history
Related to #388
  • Loading branch information
sindresorhus authored Nov 22, 2017
1 parent 9495256 commit 974473a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ function asStream(opts) {

const statusCode = res.statusCode;

res.on('error', err => {
proxy.emit('error', new got.ReadError(err, opts));
});

res.pipe(output);

if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
Expand Down
7 changes: 7 additions & 0 deletions test/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ test('handles gzip error', async t => {
t.is(err.name, 'ReadError');
});

test('handles gzip error - stream', async t => {
const err = await t.throws(getStream(got.stream(`${s.url}/corrupted`)));
t.is(err.message, 'incorrect header check');
t.is(err.path, '/corrupted');
t.is(err.name, 'ReadError');
});

test('decompress option opts out of decompressing', async t => {
const response = await got(s.url, {decompress: false});
t.true(Buffer.compare(response.body, gzipData) === 0);
Expand Down

0 comments on commit 974473a

Please sign in to comment.