-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): Return same value from startSpan as callback returns
#19300
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68b4dd8
handleCallbackErrors return type fix
s1gr1d e0629dc
with `has` object trap
s1gr1d 0ffce74
remove proxy wrapping
s1gr1d c2e7351
add tracing unit tests
s1gr1d d9d0f5e
clean up test cases
s1gr1d cad6d50
remove throw error
s1gr1d 9b8d764
fix: Copy properties onto Sentry-chained promises
isaacs 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
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
79 changes: 79 additions & 0 deletions
79
...wser-integration-tests/suites/public-api/startSpan/thenable-with-extra-methods/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,79 @@ | ||
| /** | ||
| * Test that verifies thenable objects with extra methods (like jQuery's jqXHR) | ||
| * preserve those methods when returned from Sentry.startSpan(). | ||
| * | ||
| * Example case: | ||
| * const jqXHR = Sentry.startSpan({ name: "test" }, () => $.ajax(...)); | ||
| * jqXHR.abort(); // Should work and not throw an error because of missing abort() method | ||
| */ | ||
|
|
||
| // Load jQuery from CDN | ||
| const script = document.createElement('script'); | ||
| script.src = 'https://code.jquery.com/jquery-3.7.1.min.js'; | ||
| script.integrity = 'sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo='; | ||
| script.crossOrigin = 'anonymous'; | ||
|
|
||
| script.onload = function () { | ||
| runTest(); | ||
| }; | ||
|
|
||
| script.onerror = function () { | ||
| window.jqXHRTestError = 'Failed to load jQuery'; | ||
| window.jqXHRMethodsPreserved = false; | ||
| }; | ||
|
|
||
| document.head.appendChild(script); | ||
|
|
||
| async function runTest() { | ||
| window.jqXHRAbortCalled = false; | ||
| window.jqXHRAbortResult = null; | ||
| window.jqXHRTestError = null; | ||
|
|
||
| try { | ||
| if (!window.jQuery) { | ||
| throw new Error('jQuery not loaded'); | ||
| } | ||
|
|
||
| const result = Sentry.startSpan({ name: 'test-jqxhr', op: 'http.client' }, () => { | ||
| // Make a real AJAX request with jQuery | ||
| return window.jQuery.ajax({ | ||
| url: 'https://httpbin.org/status/200', | ||
| method: 'GET', | ||
| timeout: 5000, | ||
| }); | ||
| }); | ||
|
|
||
| const hasAbort = typeof result.abort === 'function'; | ||
| const hasReadyState = 'readyState' in result; | ||
|
|
||
| if (hasAbort && hasReadyState) { | ||
| try { | ||
| result.abort(); | ||
| window.jqXHRAbortCalled = true; | ||
| window.jqXHRAbortResult = 'abort-successful'; | ||
| window.jqXHRMethodsPreserved = true; | ||
| } catch (e) { | ||
| console.log('abort() threw an error:', e); | ||
| window.jqXHRTestError = `abort() failed: ${e.message}`; | ||
| window.jqXHRMethodsPreserved = false; | ||
| } | ||
| } else { | ||
| window.jqXHRMethodsPreserved = false; | ||
| window.jqXHRTestError = 'jqXHR methods not preserved'; | ||
| } | ||
|
|
||
| // Since we aborted the request, it should be rejected | ||
| try { | ||
| await result; | ||
| window.jqXHRPromiseResolved = true; // Unexpected | ||
| } catch (err) { | ||
| // Expected: aborted request rejects | ||
| window.jqXHRPromiseResolved = false; | ||
| window.jqXHRPromiseRejected = true; | ||
| } | ||
| } catch (error) { | ||
| console.error('Test error:', error); | ||
| window.jqXHRTestError = error.message; | ||
| window.jqXHRMethodsPreserved = false; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
...browser-integration-tests/suites/public-api/startSpan/thenable-with-extra-methods/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,51 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { sentryTest } from '../../../../utils/fixtures'; | ||
| import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers'; | ||
|
|
||
| sentryTest('preserves extra methods on real jQuery jqXHR objects', async ({ getLocalTestUrl, page }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
| const transactionPromise = waitForTransactionRequest(page); | ||
|
|
||
| await page.goto(url); | ||
|
|
||
| // Wait for jQuery to load | ||
| await page.waitForTimeout(1000); | ||
|
|
||
| const methodsPreserved = await page.evaluate(() => (window as any).jqXHRMethodsPreserved); | ||
| expect(methodsPreserved).toBe(true); | ||
|
|
||
| const abortCalled = await page.evaluate(() => (window as any).jqXHRAbortCalled); | ||
| expect(abortCalled).toBe(true); | ||
|
|
||
| const abortReturnValue = await page.evaluate(() => (window as any).jqXHRAbortResult); | ||
| expect(abortReturnValue).toBe('abort-successful'); | ||
|
|
||
| const testError = await page.evaluate(() => (window as any).jqXHRTestError); | ||
| expect(testError).toBeNull(); | ||
|
|
||
| const transaction = envelopeRequestParser(await transactionPromise); | ||
| expect(transaction.transaction).toBe('test-jqxhr'); | ||
| expect(transaction.spans).toBeDefined(); | ||
| }); | ||
|
|
||
| sentryTest('aborted request rejects promise correctly', async ({ getLocalTestUrl, page }) => { | ||
| if (shouldSkipTracingTest()) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
| await page.goto(url); | ||
|
|
||
| // Wait for jQuery to load | ||
| await page.waitForTimeout(1000); | ||
|
|
||
| const promiseRejected = await page.evaluate(() => (window as any).jqXHRPromiseRejected); | ||
| expect(promiseRejected).toBe(true); | ||
|
|
||
| const promiseResolved = await page.evaluate(() => (window as any).jqXHRPromiseResolved); | ||
| expect(promiseResolved).toBe(false); | ||
| }); |
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
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,55 @@ | ||
| const isActualPromise = (p: unknown) => | ||
| p instanceof Promise && !(p as unknown as ChainedPromiseLike<unknown>)[kChainedCopy]; | ||
|
|
||
| type ChainedPromiseLike<T> = PromiseLike<T> & { | ||
| [kChainedCopy]: true; | ||
| }; | ||
| const kChainedCopy = Symbol('chained PromiseLike'); | ||
|
|
||
| /** | ||
| * Copy the properties from a decorated promiselike object onto its chained | ||
| * actual promise. | ||
| */ | ||
| export const chainAndCopyPromiseLike = <V, T extends PromiseLike<V>>( | ||
| original: T, | ||
| onSuccess: (value: V) => void, | ||
| onError: (e: unknown) => void, | ||
| ): T => { | ||
| const chained = original.then( | ||
| value => { | ||
| onSuccess(value); | ||
| return value; | ||
| }, | ||
| err => { | ||
| onError(err); | ||
| throw err; | ||
| }, | ||
| ) as T; | ||
|
|
||
| // if we're just dealing with "normal" Promise objects, return the chain | ||
| return isActualPromise(chained) && isActualPromise(original) ? chained : copyProps(original, chained); | ||
| }; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const copyProps = <T extends Record<string, any>>(original: T, chained: T): T => { | ||
|
isaacs marked this conversation as resolved.
|
||
| let mutated = false; | ||
| //oxlint-disable-next-line guard-for-in | ||
| for (const key in original) { | ||
| if (key in chained) continue; | ||
| mutated = true; | ||
| const value = original[key]; | ||
| if (typeof value === 'function') { | ||
| Object.defineProperty(chained, key, { | ||
| value: (...args: unknown[]) => value.apply(original, args), | ||
| enumerable: true, | ||
| configurable: true, | ||
| writable: true, | ||
| }); | ||
| } else { | ||
| (chained as Record<string, unknown>)[key] = value; | ||
| } | ||
| } | ||
|
|
||
| if (mutated) Object.assign(chained, { [kChainedCopy]: true }); | ||
| return chained; | ||
| }; | ||
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
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
56 changes: 56 additions & 0 deletions
56
packages/core/test/lib/utils/chain-and-copy-promiselike.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,56 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { chainAndCopyPromiseLike } from '../../../src/utils/chain-and-copy-promiselike'; | ||
|
|
||
| describe('chain and copy promiselike objects', () => { | ||
| it('does no copying for normal promises', async () => { | ||
| const p = new Promise<number>(res => res(1)); | ||
| Object.assign(p, { newProperty: true }); | ||
| let success = false; | ||
| let error = false; | ||
| const q = chainAndCopyPromiseLike( | ||
| p, | ||
| () => { | ||
| success = true; | ||
| }, | ||
| () => { | ||
| error = true; | ||
| }, | ||
| ); | ||
| expect(await q).toBe(1); | ||
| //@ts-expect-error - this is not a normal prop on Promises | ||
| expect(q.newProperty).toBe(undefined); | ||
| expect(success).toBe(true); | ||
| expect(error).toBe(false); | ||
| }); | ||
|
|
||
| it('copies properties of non-Promise then-ables', async () => { | ||
| class FakePromise<T extends unknown> { | ||
| value: T; | ||
| constructor(value: T) { | ||
| this.value = value; | ||
| } | ||
| then(fn: (value: T) => unknown) { | ||
| const newVal = fn(this.value); | ||
| return new FakePromise(newVal); | ||
| } | ||
| } | ||
| const p = new FakePromise(1) as PromiseLike<number>; | ||
| Object.assign(p, { newProperty: true }); | ||
| let success = false; | ||
| let error = false; | ||
| const q = chainAndCopyPromiseLike( | ||
| p, | ||
| () => { | ||
| success = true; | ||
| }, | ||
| () => { | ||
| error = true; | ||
| }, | ||
| ); | ||
| expect(await q).toBe(1); | ||
| //@ts-expect-error - this is not a normal prop on FakePromises | ||
| expect(q.newProperty).toBe(true); | ||
| expect(success).toBe(true); | ||
| expect(error).toBe(false); | ||
| }); | ||
| }); |
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.