Skip to content
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
22 changes: 14 additions & 8 deletions packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createTransport } from '@sentry/core';
import { Transport, TransportMakeRequestResponse, TransportRequest } from '@sentry/types';
import { rejectedSyncPromise } from '@sentry/utils';

import { BrowserTransportOptions } from './types';
import { FetchImpl, getNativeFetchImplementation } from './utils';
import { clearCachedFetchImplementation, FetchImpl, getNativeFetchImplementation } from './utils';

/**
* Creates a Transport that uses the Fetch API to send events to Sentry.
Expand Down Expand Up @@ -30,13 +31,18 @@ export function makeFetchTransport(
...options.fetchOptions,
};

return nativeFetch(options.url, requestOptions).then(response => ({
statusCode: response.status,
headers: {
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
'retry-after': response.headers.get('Retry-After'),
},
}));
try {
return nativeFetch(options.url, requestOptions).then(response => ({
statusCode: response.status,
headers: {
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
'retry-after': response.headers.get('Retry-After'),
},
}));
} catch (e) {
clearCachedFetchImplementation();
return rejectedSyncPromise(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering what the long term outcome of this change is... If we go forward with this, we will probably just hide the fact that fetch is undefined behind a network error client report. Is that what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is tricky. I also realized that this change means we don't respect rate-limiting on the SDK at all...

}
}

return createTransport(options, makeRequest);
Expand Down
7 changes: 6 additions & 1 deletion packages/browser/src/transports/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isNativeFetch, logger, WINDOW } from '@sentry/utils';

let cachedFetchImpl: FetchImpl;
let cachedFetchImpl: FetchImpl | undefined = undefined;

export type FetchImpl = typeof fetch;

Expand Down Expand Up @@ -76,3 +76,8 @@ export function getNativeFetchImplementation(): FetchImpl {
return (cachedFetchImpl = fetchImpl.bind(WINDOW));
/* eslint-enable @typescript-eslint/unbound-method */
}

/** Clears cached fetch impl */
export function clearCachedFetchImplementation(): void {
cachedFetchImpl = undefined;
}
9 changes: 9 additions & 0 deletions packages/browser/test/unit/transports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ describe('NewFetchTransport', () => {
...REQUEST_OPTIONS,
});
});

it('handles when `getNativeFetchImplementation` is undefined', async () => {
const mockFetch = jest.fn(() => undefined) as unknown as FetchImpl;
const transport = makeFetchTransport(DEFAULT_FETCH_TRANSPORT_OPTIONS, mockFetch);

expect(mockFetch).toHaveBeenCalledTimes(0);
await expect(() => transport.send(ERROR_ENVELOPE)).not.toThrow();
expect(mockFetch).toHaveBeenCalledTimes(1);
});
});