Skip to content

ref(deno): Refactor deno integration to avoid setupOnce #9900

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 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 30 additions & 38 deletions packages/deno/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ServerRuntimeClient } from '@sentry/core';
import { getClient, getCurrentHub, getCurrentScope } from '@sentry/core';
import { captureEvent } from '@sentry/core';
import { getClient } from '@sentry/core';
import { flush } from '@sentry/core';
import type { Event, Hub, Integration, Primitive, StackParser } from '@sentry/types';
import type { Client, Event, Integration, Primitive, StackParser } from '@sentry/types';
import { eventFromUnknownInput, isPrimitive } from '@sentry/utils';

type GlobalHandlersIntegrationsOptionKeys = 'error' | 'unhandledrejection';
Expand All @@ -26,15 +27,6 @@ export class GlobalHandlers implements Integration {
/** JSDoc */
private readonly _options: GlobalHandlersIntegrations;

/**
* Stores references functions to installing handlers. Will set to undefined
* after they have been run so that they are not used twice.
*/
private _installFunc: Record<GlobalHandlersIntegrationsOptionKeys, (() => void) | undefined> = {
error: installGlobalErrorHandler,
unhandledrejection: installGlobalUnhandledRejectionHandler,
};

/** JSDoc */
public constructor(options?: GlobalHandlersIntegrations) {
this._options = {
Expand All @@ -47,35 +39,35 @@ export class GlobalHandlers implements Integration {
* @inheritDoc
*/
public setupOnce(): void {
const options = this._options;

// We can disable guard-for-in as we construct the options object above + do checks against
// `this._installFunc` for the property.
// eslint-disable-next-line guard-for-in
for (const key in options) {
const installFunc = this._installFunc[key as GlobalHandlersIntegrationsOptionKeys];
if (installFunc && options[key as GlobalHandlersIntegrationsOptionKeys]) {
installFunc();
this._installFunc[key as GlobalHandlersIntegrationsOptionKeys] = undefined;
}
// noop
}

/** @inheritdoc */
public setup(client: Client): void {
if (this._options.error) {
installGlobalErrorHandler(client);
}
if (this._options.unhandledrejection) {
installGlobalUnhandledRejectionHandler(client);
}
}
}

function installGlobalErrorHandler(): void {
function installGlobalErrorHandler(client: Client): void {
globalThis.addEventListener('error', data => {
if (isExiting) {
if (getClient() !== client || isExiting) {
return;
}

const [hub, stackParser] = getHubAndOptions();
const stackParser = getStackParser();

const { message, error } = data;

const event = eventFromUnknownInput(getClient(), stackParser, error || message);

event.level = 'fatal';

hub.captureEvent(event, {
captureEvent(event, {
originalException: error,
mechanism: {
handled: false,
Expand All @@ -94,13 +86,13 @@ function installGlobalErrorHandler(): void {
});
}

function installGlobalUnhandledRejectionHandler(): void {
function installGlobalUnhandledRejectionHandler(client: Client): void {
globalThis.addEventListener('unhandledrejection', (e: PromiseRejectionEvent) => {
if (isExiting) {
if (getClient() !== client || isExiting) {
return;
}

const [hub, stackParser] = getHubAndOptions();
const stackParser = getStackParser();
let error = e;

// dig the object of the rejection out of known event types
Expand All @@ -118,7 +110,7 @@ function installGlobalUnhandledRejectionHandler(): void {

event.level = 'fatal';

hub.captureEvent(event, {
captureEvent(event, {
originalException: error,
mechanism: {
handled: false,
Expand Down Expand Up @@ -157,12 +149,12 @@ function eventFromRejectionWithPrimitive(reason: Primitive): Event {
};
}

function getHubAndOptions(): [Hub, StackParser] {
const hub = getCurrentHub();
const client = hub.getClient<ServerRuntimeClient>();
const options = (client && client.getOptions()) || {
stackParser: () => [],
attachStacktrace: false,
};
return [hub, options.stackParser];
function getStackParser(): StackParser {
const client = getClient<ServerRuntimeClient>();

if (!client) {
return () => [];
}

return client.getOptions().stackParser;
}