Skip to content

Commit

Permalink
🐛 fix telemetry error when calling fetch(null)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Mar 7, 2023
1 parent 1cc4fc7 commit 31a20fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/core/src/browser/fetchObservable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { initFetchObservable } from './fetchObservable'

describe('fetch proxy', () => {
const FAKE_URL = 'http://fake-url/'
const FAKE_RELATIVE_URL = '/fake-path'
const NORMALIZED_FAKE_RELATIVE_URL = `${location.origin}/fake-path`
let fetchStub: (input: RequestInfo, init?: RequestInit) => FetchStubPromise
let fetchStubManager: FetchStubManager
let requestsTrackingSubscription: Subscription
Expand Down Expand Up @@ -111,6 +113,7 @@ describe('fetch proxy', () => {
fetchStub(new Request(FAKE_URL, { method: 'PUT' }), { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(new Request(FAKE_URL), { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(FAKE_URL, { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(null as any).resolveWith({ status: 500 })

fetchStubManager.whenAllComplete(() => {
expect(requests[0].method).toEqual('GET')
Expand All @@ -119,17 +122,24 @@ describe('fetch proxy', () => {
expect(requests[3].method).toEqual('POST')
expect(requests[4].method).toEqual('POST')
expect(requests[5].method).toEqual('POST')
expect(requests[6].method).toEqual('GET')
done()
})
})

it('should get url from input', (done) => {
it('should get the normalized url from input', (done) => {
fetchStub(FAKE_URL).rejectWith(new Error('fetch error'))
fetchStub(new Request(FAKE_URL)).rejectWith(new Error('fetch error'))
fetchStub(null as any).rejectWith(new Error('fetch error'))
fetchStub(FAKE_RELATIVE_URL).rejectWith(new Error('fetch error'))
fetchStub(new Request(FAKE_RELATIVE_URL)).rejectWith(new Error('fetch error'))

fetchStubManager.whenAllComplete(() => {
expect(requests[0].url).toEqual(FAKE_URL)
expect(requests[1].url).toEqual(FAKE_URL)
expect(requests[2].url).toMatch(/\/null$/)
expect(requests[3].url).toEqual(NORMALIZED_FAKE_RELATIVE_URL)
expect(requests[4].url).toEqual(NORMALIZED_FAKE_RELATIVE_URL)
done()
})
})
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/browser/fetchObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function createFetchObservable() {
}

function beforeSend(observable: Observable<FetchContext>, input: RequestInfo, init?: RequestInit) {
const method = (init && init.method) || (typeof input === 'object' && input.method) || 'GET'
const url = normalizeUrl((typeof input === 'object' && input.url) || (input as string))
const method = (init && init.method) || (input instanceof Request && input.method) || 'GET'
const url = input instanceof Request ? input.url : normalizeUrl(input)
const startClocks = clocksNow()

const context: FetchStartContext = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,30 @@ describe('resourceCollection', () => {
})
})

it('should create resource from completed fetch request', () => {
if (isIE()) {
pending('No IE support')
}
const { lifeCycle, rawRumEvents } = setupBuilder.build()
lifeCycle.notify(
LifeCycleEventType.REQUEST_COMPLETED,
createCompletedRequest({
type: RequestType.FETCH,
input: null as any,
})
)

expect(rawRumEvents.length).toBe(1)
expect(rawRumEvents[0].domainContext).toEqual({
performanceEntry: undefined,
xhr: undefined,
response: undefined,
requestInput: null as any,
requestInit: undefined,
error: undefined,
})
})

it('should include the error in failed fetch requests', () => {
const { lifeCycle, rawRumEvents } = setupBuilder.build()
const error = new Error()
Expand Down

0 comments on commit 31a20fe

Please sign in to comment.