Skip to content

Commit

Permalink
🐛[RUMF-293][fetch] handle fetch response text error (#252)
Browse files Browse the repository at this point in the history
When consuming a fetch response body, it can be disturbed or locked, a rejected promise is then returned (cf https://fetch.spec.whatwg.org/#concept-body-consume-body).
This rejected promise was displayed on the browser console and the corresponding request was not collected.

Handle the rejected promise and add the corresponding error to the collected request response.
  • Loading branch information
bcaudan committed Feb 6, 2020
1 parent 5037108 commit ce037b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/requestCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ export function trackFetch(observable: RequestObservable) {
type: RequestType.FETCH,
})
} else if ('status' in response) {
const text = await response.clone().text()
let text: string
try {
text = await response.clone().text()
} catch (e) {
text = `Unable to retrieve response: ${e}`
}
observable.notify({
duration,
method,
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ export class FetchStubBuilder {
resolve({
...response,
clone: () => {
const cloned = { text: async () => response.responseText }
const cloned = {
text: async () => {
if (response.responseTextError) {
throw response.responseTextError
}
return response.responseText
},
}
return cloned as Response
},
}) as Promise<ResponseStub>
Expand All @@ -67,6 +74,7 @@ export class FetchStubBuilder {

export interface ResponseStub extends Partial<Response> {
responseText?: string
responseTextError?: Error
}

export type FetchStub = (input: RequestInfo, init?: RequestInit) => FetchStubPromise
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/requestCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ describe('fetch tracker', () => {
})
})

// https://fetch.spec.whatwg.org/#concept-body-consume-body
it('should track fetch with response text error', (done) => {
fetchStub(FAKE_URL).resolveWith({ status: 200, responseTextError: new Error('locked') })

fetchStubBuilder.whenAllComplete((requests: RequestDetails[]) => {
const request = requests[0]
expect(request.type).toEqual(RequestType.FETCH)
expect(request.method).toEqual('GET')
expect(request.url).toEqual(FAKE_URL)
expect(request.status).toEqual(200)
expect(request.response).toMatch(/Error: locked/)
expect(isRejected(request)).toBe(false)
expect(isServerError(request)).toBe(false)
done()
})
})

it('should track opaque fetch', (done) => {
// https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
fetchStub(FAKE_URL).resolveWith({ status: 0, type: 'opaque' })
Expand Down

0 comments on commit ce037b8

Please sign in to comment.