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

🐛[RUMF-293][fetch] handle fetch response text error #252

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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