-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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(utils): Handle when requests get aborted in fetch instrumentation #13202
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7da58d1
fix(utils): Handle when requests get aborted
AbhiPrasad 6cdcd73
test: Add browser integration test for AbortController
chargome ac407b3
unflake
lforst a7c8c1b
Merge remote-tracking branch 'origin/develop' into abhi-fetch-abort-c…
lforst 43c6200
Assert on console log
lforst cd5e89d
this time actually
lforst 62f5fbd
skip non-tracing tests
lforst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...rowser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
tracesSampleRate: 1, | ||
}); |
36 changes: 36 additions & 0 deletions
36
...ser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
let controller; | ||
|
||
const startFetch = e => { | ||
controller = new AbortController(); | ||
const { signal } = controller; | ||
|
||
Sentry.startSpan( | ||
{ | ||
name: 'with-abort-controller', | ||
forceTransaction: true, | ||
}, | ||
async () => { | ||
await 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); |
10 changes: 10 additions & 0 deletions
10
...-integration-tests/suites/integrations/httpclient/fetch/withAbortController/template.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
41 changes: 41 additions & 0 deletions
41
...rowser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event as SentryEvent } from '@sentry/types'; | ||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should handle aborted fetch calls', async ({ getLocalTestPath, page }) => { | ||
if (shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
await page.route('**/foo', async () => { | ||
// never fulfil this route because we abort the request as part of the test | ||
}); | ||
|
||
const transactionEventPromise = getFirstSentryEnvelopeRequest<SentryEvent>(page); | ||
|
||
const hasAbortedFetchPromise = new Promise<void>(resolve => { | ||
page.on('console', msg => { | ||
if (msg.type() === 'log' && msg.text() === 'Fetch aborted') { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
await page.goto(url); | ||
|
||
await page.locator('[data-test-id=start-button]').click(); | ||
await page.locator('[data-test-id=abort-button]').click(); | ||
|
||
const transactionEvent = await transactionEventPromise; | ||
|
||
// assert that fetch calls do not return undefined | ||
const fetchBreadcrumbs = transactionEvent.breadcrumbs?.filter( | ||
({ category, data }) => category === 'fetch' && data === undefined, | ||
); | ||
expect(fetchBreadcrumbs).toHaveLength(0); | ||
|
||
await expect(hasAbortedFetchPromise).resolves.toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.