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

🐛 Handle non-object response and error #2860

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 28 additions & 2 deletions packages/core/src/browser/fetchObservable.spec.ts
Copy link
Collaborator

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('fetch proxy', () => {
})
})

it('should keep promise resolved behavior', (done) => {
it('should keep promise resolved behavior for Response', (done) => {
const mockFetchPromise = fetch(FAKE_URL)
const spy = jasmine.createSpy()
mockFetchPromise.then(spy).catch(() => {
Expand All @@ -176,7 +176,21 @@ describe('fetch proxy', () => {
})
})

it('should keep promise rejected behavior', (done) => {
it('should keep promise resolved behavior for any other type', (done) => {
const mockFetchPromise = fetch(FAKE_URL)
const spy = jasmine.createSpy()
mockFetchPromise.then(spy).catch(() => {
fail('Should not have thrown an error!')
})
mockFetchPromise.resolveWith('response' as any)

setTimeout(() => {
expect(spy).toHaveBeenCalled()
done()
})
})

it('should keep promise rejected behavior for Error', (done) => {
const mockFetchPromise = fetch(FAKE_URL)
const spy = jasmine.createSpy()
mockFetchPromise.catch(spy)
Expand All @@ -188,6 +202,18 @@ describe('fetch proxy', () => {
})
})

it('should keep promise rejected behavior for any other type', (done) => {
const mockFetchPromise = fetch(FAKE_URL)
const spy = jasmine.createSpy()
mockFetchPromise.catch(spy)
mockFetchPromise.rejectWith('fetch error' as any)

setTimeout(() => {
expect(spy).toHaveBeenCalled()
done()
})
})

it('should allow to enhance the context', (done) => {
type CustomContext = FetchContext & { foo: string }
contextEditionSubscription = initFetchObservable().subscribe((rawContext) => {
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/browser/fetchObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
)
Copy link
Member

Choose a reason for hiding this comment

The 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
responsePromise.then(
monitor((response) => reportFetch(response)),
monitor((error) => reportFetch(error, true))
)
function reportFetch(partialContext: Partial<FetchResolveContext>) {
const context = startContext as unknown as FetchResolveContext
context.state = 'resolve'
assign(context, partial)
observable.notify(context)
}
responsePromise.then(
monitor((response) => {
reportFetch({
response,
responsType: response.type,
status: response.status,
isAborted: false
})
}),
monitor((error: unknown) => {
reportFetch({
status: 0,
isAborted: response instanceof DOMException && response.code === DOMException.ABORT_ERR,
error
})
})
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and added support for AbortController.

}