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

fix(browser): Handle case where fetch can be undefined #5973

Merged
merged 3 commits into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/browser/src/transports/fetch.ts
@@ -1,5 +1,6 @@
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';
Expand Down Expand Up @@ -30,13 +31,17 @@ 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) {
return rejectedSyncPromise(e);
Copy link
Member

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
Member 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
9 changes: 9 additions & 0 deletions packages/browser/test/unit/transports/fetch.test.ts
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);
});
});