-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(utils): Handle when requests get aborted in fetch instrumentation #13202
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
Changes from 2 commits
7da58d1
6cdcd73
ac407b3
a7c8c1b
43c6200
cd5e89d
62f5fbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [Sentry.browserTracingIntegration()], | ||
| tracesSampleRate: 1, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| let controller; | ||
|
|
||
| const startFetch = e => { | ||
| controller = new AbortController(); | ||
| const { signal } = controller; | ||
|
|
||
| fetch('http://localhost:7654/foo', { signal }) | ||
| .then(response => response.json()) | ||
| .then(data => { | ||
| console.log('Fetch succeeded:', data); | ||
| }) | ||
| .catch(err => { | ||
| if (err.name === 'AbortError') { | ||
| console.log('Fetch aborted'); | ||
| } else { | ||
| console.error('Fetch error:', err); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const abortFetch = e => { | ||
| if (controller) { | ||
| controller.abort(); | ||
| } | ||
| }; | ||
|
|
||
| document.querySelector('[data-test-id=start-button]').addEventListener('click', startFetch); | ||
| document.querySelector('[data-test-id=abort-button]').addEventListener('click', abortFetch); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button data-test-id="start-button">Start fetch</button> | ||
| <button data-test-id="abort-button">Abort fetch</button> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { Event as SentryEvent } from '@sentry/types'; | ||
| import { sentryTest } from '../../../../../utils/fixtures'; | ||
| import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
|
||
| sentryTest('should handle aborted fetch calls', async ({ getLocalTestPath, page }) => { | ||
| const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
|
||
| await page.route('**/foo', async route => { | ||
| setTimeout(() => { | ||
| route.fulfill({ | ||
| status: 200, | ||
| contentType: 'application/json', | ||
| body: JSON.stringify({ message: 'This is the server response' }), | ||
| }); | ||
| }, 2000); | ||
| }); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| await page.locator('[data-test-id=start-button]').click(); | ||
| await page.waitForTimeout(500); | ||
chargome marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await page.locator('[data-test-id=abort-button]').click(); | ||
| const eventData = await getFirstSentryEnvelopeRequest<SentryEvent>(page); | ||
|
|
||
| // assert that fetch calls do not return undefined | ||
| const fetchBreadcrumbs = eventData.breadcrumbs?.filter( | ||
| ({ category, data }) => category === 'fetch' && data === undefined, | ||
| ); | ||
| expect(fetchBreadcrumbs).toHaveLength(0); | ||
|
|
||
| // assert that fetch call has been aborted | ||
| const abortedBreadcrumb = eventData.breadcrumbs?.filter( | ||
| ({ category, message }) => category === 'console' && message === 'Fetch aborted', | ||
| ); | ||
| expect(abortedBreadcrumb).toHaveLength(1); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,47 +80,42 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat | |
| if (onFetchResolved) { | ||
| onFetchResolved(response); | ||
| } else { | ||
| const finishedHandlerData: HandlerDataFetch = { | ||
| triggerHandlers('fetch', { | ||
| ...handlerData, | ||
| endTimestamp: timestampInSeconds() * 1000, | ||
| response, | ||
| }; | ||
| triggerHandlers('fetch', finishedHandlerData); | ||
| }); | ||
| } | ||
|
|
||
| return response; | ||
| }, | ||
| (error: Error) => { | ||
| if (!onFetchResolved) { | ||
| const erroredHandlerData: HandlerDataFetch = { | ||
| ...handlerData, | ||
| endTimestamp: timestampInSeconds() * 1000, | ||
| error, | ||
| }; | ||
|
|
||
| triggerHandlers('fetch', erroredHandlerData); | ||
|
|
||
| if (isError(error) && error.stack === undefined) { | ||
| // NOTE: If you are a Sentry user, and you are seeing this stack frame, | ||
| // it means the error, that was caused by your fetch call did not | ||
| // have a stack trace, so the SDK backfilled the stack trace so | ||
| // you can see which fetch call failed. | ||
| error.stack = virtualStackTrace; | ||
| addNonEnumerableProperty(error, 'framesToPop', 1); | ||
| } | ||
| triggerHandlers('fetch', { | ||
| ...handlerData, | ||
| endTimestamp: timestampInSeconds() * 1000, | ||
| error, | ||
| }); | ||
|
|
||
| if (isError(error) && error.stack === undefined) { | ||
| // NOTE: If you are a Sentry user, and you are seeing this stack frame, | ||
| // it means the sentry.javascript SDK caught an error invoking your application code. | ||
| // This is expected behavior and NOT indicative of a bug with sentry.javascript. | ||
| throw error; | ||
| // it means the error, that was caused by your fetch call did not | ||
| // have a stack trace, so the SDK backfilled the stack trace so | ||
| // you can see which fetch call failed. | ||
| error.stack = virtualStackTrace; | ||
| addNonEnumerableProperty(error, 'framesToPop', 1); | ||
| } | ||
|
|
||
| // NOTE: If you are a Sentry user, and you are seeing this stack frame, | ||
| // it means the sentry.javascript SDK caught an error invoking your application code. | ||
| // This is expected behavior and NOT indicative of a bug with sentry.javascript. | ||
| throw error; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lforst this is a breaking change for apps that do not handle fetch exceptions :( as they did not get the exceptions before and now they do....
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate? Ideally in a new issue. I do not think this alters behaviour. This actually fixes a bug where we swallowed errors. |
||
| }, | ||
| ); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| function resolveResponse(res: Response | undefined, onFinishedResolving: () => void): void { | ||
| async function resolveResponse(res: Response | undefined, onFinishedResolving: () => void): Promise<void> { | ||
| if (res && res.body) { | ||
| const responseReader = res.body.getReader(); | ||
|
|
||
|
|
@@ -146,25 +141,21 @@ function resolveResponse(res: Response | undefined, onFinishedResolving: () => v | |
| } | ||
| } | ||
|
|
||
| responseReader | ||
| return responseReader | ||
| .read() | ||
| .then(consumeChunks) | ||
| .then(() => { | ||
| onFinishedResolving(); | ||
| }) | ||
| .catch(() => { | ||
| // noop | ||
| }); | ||
| .then(onFinishedResolving) | ||
| .catch(() => undefined); | ||
| } | ||
| } | ||
|
|
||
| async function streamHandler(response: Response): Promise<void> { | ||
| // clone response for awaiting stream | ||
| let clonedResponseForResolving: Response | undefined; | ||
| let clonedResponseForResolving: Response; | ||
| try { | ||
| clonedResponseForResolving = response.clone(); | ||
| } catch (e) { | ||
| // noop | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| await resolveResponse(clonedResponseForResolving, () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.