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 5 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
37 changes: 24 additions & 13 deletions packages/core/src/browser/fetchObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { InstrumentedMethodCall } from '../tools/instrumentMethod'
import { instrumentMethod } from '../tools/instrumentMethod'
import { monitor } from '../tools/monitor'
import { Observable } from '../tools/observable'
import { assign } from '../tools/utils/polyfills'
import type { ClocksState } from '../tools/utils/timeUtils'
import { clocksNow } from '../tools/utils/timeUtils'
import { normalizeUrl } from '../tools/utils/urlPolyfill'
Expand Down Expand Up @@ -96,21 +97,31 @@ function afterSend(
responsePromise: Promise<Response>,
startContext: FetchStartContext
) {
const reportFetch = (response: Response | Error) => {
const context = startContext as unknown as FetchResolveContext
const context = startContext as unknown as FetchResolveContext

function reportFetch(partialContext: Partial<FetchResolveContext>) {
context.state = 'resolve'
if ('stack' in response || response instanceof Error) {
context.status = 0
context.isAborted = response instanceof DOMException && response.code === DOMException.ABORT_ERR
context.error = response
} else if ('status' in response) {
context.response = response
context.responseType = response.type
context.status = response.status
context.isAborted = false
}
assign(context, partialContext)
observable.notify(context)
}

responsePromise.then(monitor(reportFetch), monitor(reportFetch))
responsePromise.then(
monitor((response) => {
reportFetch({
response,
responseType: response.type,
status: response.status,
isAborted: false,
})
}),
monitor((error: Error) => {
const isAborted =
Copy link
Collaborator

Choose a reason for hiding this comment

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

🥜 nitpick: ‏You could inline it

    reportFetch({
        status: 0,
        isAborted:
          context.init?.signal?.aborted || (error instanceof DOMException && error.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

context.init?.signal?.aborted || (error instanceof DOMException && error.code === DOMException.ABORT_ERR)
reportFetch({
status: 0,
isAborted,
error,
})
})
)
}