-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(core): Refactor core integrations to functional style (#9916)
Also make a small adjustment to the legacy converter util to ensure `setupOnce()` is callable with or without arguments, to remain stable.
- Loading branch information
Showing
7 changed files
with
201 additions
and
252 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,33 @@ | ||
import type { Integration, WrappedFunction } from '@sentry/types'; | ||
import type { IntegrationFn, WrappedFunction } from '@sentry/types'; | ||
import { getOriginalFunction } from '@sentry/utils'; | ||
import { convertIntegrationFnToClass } from '../integration'; | ||
|
||
let originalFunctionToString: () => void; | ||
|
||
/** Patch toString calls to return proper name for wrapped functions */ | ||
export class FunctionToString implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'FunctionToString'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string; | ||
const INTEGRATION_NAME = 'FunctionToString'; | ||
|
||
public constructor() { | ||
this.name = FunctionToString.id; | ||
} | ||
const functionToStringIntegration: IntegrationFn = () => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
originalFunctionToString = Function.prototype.toString; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public setupOnce(): void { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
originalFunctionToString = Function.prototype.toString; | ||
// intrinsics (like Function.prototype) might be immutable in some environments | ||
// e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string { | ||
const context = getOriginalFunction(this) || this; | ||
return originalFunctionToString.apply(context, args); | ||
}; | ||
} catch { | ||
// ignore errors here, just don't patch this | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
// intrinsics (like Function.prototype) might be immutable in some environments | ||
// e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string { | ||
const context = getOriginalFunction(this) || this; | ||
return originalFunctionToString.apply(context, args); | ||
}; | ||
} catch { | ||
// ignore errors here, just don't patch this | ||
} | ||
} | ||
} | ||
/** Patch toString calls to return proper name for wrapped functions */ | ||
// eslint-disable-next-line deprecation/deprecation | ||
export const FunctionToString = convertIntegrationFnToClass(INTEGRATION_NAME, functionToStringIntegration); |
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 |
---|---|---|
@@ -1,59 +1,39 @@ | ||
import type { Client, Event, EventHint, Integration } from '@sentry/types'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { applyAggregateErrorsToEvent, exceptionFromError } from '@sentry/utils'; | ||
import { convertIntegrationFnToClass } from '../integration'; | ||
|
||
interface LinkedErrorsOptions { | ||
key?: string; | ||
limit?: number; | ||
} | ||
|
||
const DEFAULT_KEY = 'cause'; | ||
const DEFAULT_LIMIT = 5; | ||
|
||
/** Adds SDK info to an event. */ | ||
export class LinkedErrors implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'LinkedErrors'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public readonly name: string; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
private readonly _key: string; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
private readonly _limit: number; | ||
const INTEGRATION_NAME = 'LinkedErrors'; | ||
|
||
const linkedErrorsIntegration: IntegrationFn = (options: LinkedErrorsOptions = {}) => { | ||
const limit = options.limit || DEFAULT_LIMIT; | ||
const key = options.key || DEFAULT_KEY; | ||
|
||
return { | ||
name: INTEGRATION_NAME, | ||
preprocessEvent(event, hint, client) { | ||
const options = client.getOptions(); | ||
|
||
applyAggregateErrorsToEvent( | ||
exceptionFromError, | ||
options.stackParser, | ||
options.maxValueLength, | ||
key, | ||
limit, | ||
event, | ||
hint, | ||
); | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public constructor(options: { key?: string; limit?: number } = {}) { | ||
this._key = options.key || DEFAULT_KEY; | ||
this._limit = options.limit || DEFAULT_LIMIT; | ||
this.name = LinkedErrors.id; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public setupOnce(): void { | ||
// noop | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public preprocessEvent(event: Event, hint: EventHint | undefined, client: Client): void { | ||
const options = client.getOptions(); | ||
|
||
applyAggregateErrorsToEvent( | ||
exceptionFromError, | ||
options.stackParser, | ||
options.maxValueLength, | ||
this._key, | ||
this._limit, | ||
event, | ||
hint, | ||
); | ||
} | ||
} | ||
/** Adds SDK info to an event. */ | ||
// eslint-disable-next-line deprecation/deprecation | ||
export const LinkedErrors = convertIntegrationFnToClass(INTEGRATION_NAME, linkedErrorsIntegration); |
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.