-
-
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
ref(types): Avoid some any
type casting around wrap
code
#14463
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions
11
...integration-tests/suites/public-api/instrumentation/eventListener/event-target/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,11 @@ | ||
const btn = document.createElement('button'); | ||
btn.id = 'btn'; | ||
document.body.appendChild(btn); | ||
|
||
const functionListener = function () { | ||
throw new Error('event_listener_error'); | ||
}; | ||
|
||
btn.addEventListener('click', functionListener); | ||
|
||
btn.click(); |
29 changes: 29 additions & 0 deletions
29
...er-integration-tests/suites/public-api/instrumentation/eventListener/event-target/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,29 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers'; | ||
|
||
sentryTest('should capture target name in mechanism data', async ({ getLocalTestUrl, page }) => { | ||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'event_listener_error', | ||
mechanism: { | ||
type: 'instrument', | ||
handled: false, | ||
data: { | ||
function: 'addEventListener', | ||
handler: 'functionListener', | ||
target: 'EventTarget', | ||
}, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -31,6 +31,21 @@ export function ignoreNextOnError(): void { | |
}); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
type WrappableFunction = Function; | ||
|
||
export function wrap<T extends WrappableFunction>( | ||
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. This ensures we have proper type inferrence, so e.g. const a = wrap(1);
// a is correctly inferred as `1` now |
||
fn: T, | ||
options?: { | ||
mechanism?: Mechanism; | ||
}, | ||
): WrappedFunction<T>; | ||
export function wrap<NonFunction>( | ||
fn: NonFunction, | ||
options?: { | ||
mechanism?: Mechanism; | ||
}, | ||
): NonFunction; | ||
/** | ||
* Instruments the given function and sends an event to Sentry every time the | ||
* function throws an exception. | ||
|
@@ -40,29 +55,31 @@ export function ignoreNextOnError(): void { | |
* @returns The wrapped function. | ||
* @hidden | ||
*/ | ||
export function wrap( | ||
fn: WrappedFunction, | ||
export function wrap<T extends WrappableFunction, NonFunction>( | ||
fn: T | NonFunction, | ||
options: { | ||
mechanism?: Mechanism; | ||
} = {}, | ||
before?: WrappedFunction, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): any { | ||
): NonFunction | WrappedFunction<T> { | ||
// for future readers what this does is wrap a function and then create | ||
// a bi-directional wrapping between them. | ||
// | ||
// example: wrapped = wrap(original); | ||
// original.__sentry_wrapped__ -> wrapped | ||
// wrapped.__sentry_original__ -> original | ||
|
||
if (typeof fn !== 'function') { | ||
function isFunction(fn: T | NonFunction): fn is T { | ||
return typeof fn === 'function'; | ||
} | ||
|
||
if (!isFunction(fn)) { | ||
return fn; | ||
} | ||
|
||
try { | ||
// if we're dealing with a function that was previously wrapped, return | ||
// the original wrapper. | ||
const wrapper = fn.__sentry_wrapped__; | ||
const wrapper = (fn as WrappedFunction<T>).__sentry_wrapped__; | ||
if (wrapper) { | ||
if (typeof wrapper === 'function') { | ||
return wrapper; | ||
|
@@ -84,18 +101,12 @@ export function wrap( | |
return fn; | ||
} | ||
|
||
/* eslint-disable prefer-rest-params */ | ||
// Wrap the function itself | ||
// It is important that `sentryWrapped` is not an arrow function to preserve the context of `this` | ||
const sentryWrapped: WrappedFunction = function (this: unknown): void { | ||
const args = Array.prototype.slice.call(arguments); | ||
|
||
const sentryWrapped = function (this: unknown, ...args: unknown[]): unknown { | ||
try { | ||
if (before && typeof before === 'function') { | ||
before.apply(this, arguments); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access | ||
const wrappedArguments = args.map((arg: any) => wrap(arg, options)); | ||
// Also wrap arguments that are themselves functions | ||
const wrappedArguments = args.map(arg => wrap(arg, options)); | ||
|
||
// Attempt to invoke user-land function | ||
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it | ||
|
@@ -125,18 +136,19 @@ export function wrap( | |
|
||
throw ex; | ||
} | ||
}; | ||
/* eslint-enable prefer-rest-params */ | ||
} as unknown as WrappedFunction<T>; | ||
|
||
// Accessing some objects may throw | ||
// ref: https://github.com/getsentry/sentry-javascript/issues/1168 | ||
// Wrap the wrapped function in a proxy, to ensure any other properties of the original function remain available | ||
try { | ||
for (const property in fn) { | ||
if (Object.prototype.hasOwnProperty.call(fn, property)) { | ||
sentryWrapped[property] = fn[property]; | ||
sentryWrapped[property as keyof T] = fn[property as keyof T]; | ||
} | ||
} | ||
} catch (_oO) {} // eslint-disable-line no-empty | ||
} catch { | ||
// Accessing some objects may throw | ||
// ref: https://github.com/getsentry/sentry-javascript/issues/1168 | ||
} | ||
|
||
// Signal that this function has been wrapped/filled already | ||
// for both debugging and to prevent it to being wrapped/filled twice | ||
|
@@ -146,16 +158,19 @@ export function wrap( | |
|
||
// Restore original function name (not all browsers allow that) | ||
try { | ||
const descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name') as PropertyDescriptor; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name')!; | ||
if (descriptor.configurable) { | ||
Object.defineProperty(sentryWrapped, 'name', { | ||
get(): string { | ||
return fn.name; | ||
}, | ||
}); | ||
} | ||
// eslint-disable-next-line no-empty | ||
} catch (_oO) {} | ||
} catch { | ||
// This may throw if e.g. the descriptor does not exist, or a browser does not allow redefining `name`. | ||
// to save some bytes we simply try-catch this | ||
} | ||
|
||
return sentryWrapped; | ||
} |
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.
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.
Just an alias to avoid having ban-types eslint disable everywhere