Skip to content

Commit

Permalink
Fix unhandled The server aborted pending request rejection
Browse files Browse the repository at this point in the history
Fixes #1308
  • Loading branch information
szmarczak committed Jul 3, 2020
1 parent 04f3ea4 commit 728aef9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
11 changes: 10 additions & 1 deletion source/as-promise/core.ts
Expand Up @@ -8,7 +8,12 @@ import {
ParseError,
Response
} from './types';
import Request, {knownHookEvents, RequestError, Method, ParseJsonFunction} from '../core';
import Request, {
knownHookEvents,
RequestError,
Method,
ParseJsonFunction
} from '../core';

if (!knownHookEvents.includes('beforeRetry' as any)) {
knownHookEvents.push('beforeRetry' as any, 'afterResponse' as any);
Expand Down Expand Up @@ -146,6 +151,10 @@ export default class PromisableRequest extends Request {
}

async _beforeError(error: Error): Promise<void> {
if (this.destroyed) {
return;
}

if (!(error instanceof RequestError)) {
error = new RequestError(error.message, error, this);
}
Expand Down
2 changes: 1 addition & 1 deletion source/core/index.ts
Expand Up @@ -1079,7 +1079,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
response.once('aborted', () => {
this._beforeError(new ReadError({
name: 'Error',
message: 'The server aborted the pending request'
message: 'The server aborted pending request'
}, this));
});

Expand Down
2 changes: 1 addition & 1 deletion test/http.ts
Expand Up @@ -223,7 +223,7 @@ test('throws an error if the server aborted the request', withServer, async (t,
});

await t.throwsAsync(got(''), {
message: 'The server aborted the pending request'
message: 'The server aborted pending request'
});
});

Expand Down
13 changes: 13 additions & 0 deletions test/promise.ts
Expand Up @@ -37,3 +37,16 @@ test('returns buffer on compressed response', withServer, async (t, server, got)
const {body} = await got({decompress: false});
t.true(Buffer.isBuffer(body));
});

test('no unhandled `The server aborted pending request` rejection', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 503;
response.write('asdf');

setTimeout(() => {
response.end();
}, 100);
});

await t.throwsAsync(got(''));
});

0 comments on commit 728aef9

Please sign in to comment.