-
Notifications
You must be signed in to change notification settings - Fork 143
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
🐛 Handle non-object response and error #2860
Changes from 4 commits
8a8008e
8df8afe
4920001
1936102
d198df2
ab08d8e
bdc7b51
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -96,10 +96,17 @@ function afterSend( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
responsePromise: Promise<Response>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
startContext: FetchStartContext | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const reportFetch = (response: Response | Error) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const reportFetch = (response: Response | Error, isRejected: boolean = false) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const context = startContext as unknown as FetchResolveContext | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.state = 'resolve' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ('stack' in response || response instanceof Error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// When fetch is instrumented to return something else than a Response | Error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (typeof response !== 'object') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (isRejected) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.status = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.error = response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if ('stack' in response || response instanceof Error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.status = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.isAborted = response instanceof DOMException && response.code === DOMException.ABORT_ERR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.error = response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -112,5 +119,8 @@ function afterSend( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
observable.notify(context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
responsePromise.then(monitor(reportFetch), monitor(reportFetch)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
responsePromise.then( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
monitor((response) => reportFetch(response)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
monitor((error) => reportFetch(error, true)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. 💭 thought: Thank you for doing this! I don't think there is a good reason to have all those checks. I think we could remove them, this would allow a simpler approach:
Suggested change
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. Done, and added support for AbortController. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
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.
It could be nice to have a test with AbortSignal :)
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.
Done