diff --git a/source/core/index.ts b/source/core/index.ts index 885e06006..f348269b6 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -968,6 +968,13 @@ export default class Request extends Duplex implements RequestEvents { this._beforeError(new ReadError(error, options, response as Response)); }); + response.once('aborted', () => { + this._beforeError(new ReadError({ + name: 'Error', + message: 'The server aborted the pending request' + }, options, response as Response)); + }); + this.emit('downloadProgress', this.downloadProgress); const rawCookies = response.headers['set-cookie']; diff --git a/test/http.ts b/test/http.ts index 728fdc443..fb90a9683 100644 --- a/test/http.ts +++ b/test/http.ts @@ -183,3 +183,24 @@ test('the response contains timings property', withServer, async (t, server, got t.truthy(timings); t.true(timings.phases.total! >= 0); }); + +test('throws an error if the server aborted the request', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.writeHead(200, { + 'content-type': 'text/plain' + }); + response.write('chunk 1'); + + setImmediate(() => { + response.write('chunk 2'); + + setImmediate(() => { + response.destroy(); + }); + }); + }); + + await t.throwsAsync(got(''), { + message: 'The server aborted the pending request' + }); +});