-
-
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
Merged
Merged
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions
9
...rowser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/init.js
This file contains hidden or 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,9 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
integrations: [Sentry.browserTracingIntegration()], | ||
tracesSampleRate: 1, | ||
}); |
28 changes: 28 additions & 0 deletions
28
...ser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/subject.js
This file contains hidden or 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,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); |
10 changes: 10 additions & 0 deletions
10
...-integration-tests/suites/integrations/httpclient/fetch/withAbortController/template.html
This file contains hidden or 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> |
37 changes: 37 additions & 0 deletions
37
...rowser-integration-tests/suites/integrations/httpclient/fetch/withAbortController/test.ts
This file contains hidden or 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,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.
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); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.