Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix an issue where failed http requests could cause an unhandled promise rejection. #374

Merged
merged 3 commits into from
Feb 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ describe('given a default instance of NodeRequests', () => {
let resolve: (value: TestRequestData | PromiseLike<TestRequestData>) => void;
let promise: Promise<TestRequestData>;
let server: http.Server;
let resetResolve: () => void;
let resetPromise: Promise<void>;

beforeEach(() => {
resetPromise = new Promise((res) => {
resetResolve = res;
});

promise = new Promise<TestRequestData>((res) => {
resolve = res;
});
Expand All @@ -43,6 +49,14 @@ describe('given a default instance of NodeRequests', () => {
} else if ((req.url?.indexOf('404') || -1) >= 0) {
res.statusCode = 404;
res.end();
} else if (req.url?.includes('reset')) {
res.statusCode = 200;
res.flushHeaders();
res.write('potato');
setTimeout(() => {
res.destroy();
resetResolve();
}, 0);
} else {
res.end(TEXT_RESPONSE);
}
Expand Down Expand Up @@ -115,4 +129,19 @@ describe('given a default instance of NodeRequests', () => {
expect(serverResult.body).toEqual('BODY TEXT');
expect(serverResult.headers['sample-header']).toEqual('Some header value');
});

it('rejection is handled for response even if not awaited', async () => {
const res = await requests.fetch(`http://localhost:${PORT}/reset`);
expect(res.status).toEqual(200);
await resetPromise;
});

it('rejection is propagated with json promise', async () => {
const res = await requests.fetch(`http://localhost:${PORT}/reset`);
expect(res.status).toEqual(200);

await expect(async () => {
await res.json();
}).rejects.toThrow();
});
});
21 changes: 18 additions & 3 deletions packages/sdk/server-node/src/platform/NodeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class NodeResponse implements platform.Response {

status: number;

listened: boolean = false;

rejection?: Error;

constructor(res: http.IncomingMessage) {
this.headers = new HeaderWrapper(res.headers);
// Status code is optionally typed, but will always be present for this
Expand All @@ -28,7 +32,10 @@ export default class NodeResponse implements platform.Response {
});

res.on('error', (err) => {
reject(err);
this.rejection = err;
if (this.listened) {
reject(err);
}
});

res.on('end', () => {
Expand All @@ -37,12 +44,20 @@ export default class NodeResponse implements platform.Response {
});
}

text(): Promise<string> {
private async wrappedWait(): Promise<string> {
this.listened = true;
if (this.rejection) {
throw this.rejection;
}
return this.promise;
}

text(): Promise<string> {
return this.wrappedWait();
}

async json(): Promise<any> {
const stringValue = await this.promise;
const stringValue = await this.wrappedWait();
return JSON.parse(stringValue);
}
}
Loading