-
Notifications
You must be signed in to change notification settings - Fork 31
fix: Fix an issue where failed http requests could cause an unhandled promise rejection. #371
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,9 @@ export default class NodeResponse implements platform.Response { | |
|
|
||
| status: number; | ||
|
|
||
| listened: boolean = false; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically we don't attempt to read the body from all HTTP requests we make. Event posting doesn't return a body, so we discard the inner response. Unfortunately that inner response may still be rejected. So we track if the promise has been listened to. If it has been, then we reject like normal. If not we cache the rejection error and throw it when someone listens to the promise. |
||
| rejection?: Error; | ||
|
|
||
| constructor(res: http.IncomingMessage) { | ||
| this.headers = new HeaderWrapper(res.headers); | ||
| // Status code is optionally typed, but will always be present for this | ||
|
|
@@ -28,7 +31,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', () => { | ||
|
|
@@ -37,12 +43,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); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running this test against the original code will fail, against this code it works.