Skip to content

Commit

Permalink
fix: Ensure all logs are wrapped with consoleSandbox (#13690)
Browse files Browse the repository at this point in the history
To avoid infinite loops etc.
  • Loading branch information
mydea authored Sep 13, 2024
1 parent 69e7427 commit 5d0094a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/angular/src/errorhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Inject, Injectable } from '@angular/core';
import * as Sentry from '@sentry/browser';
import type { ReportDialogOptions } from '@sentry/browser';
import type { Event } from '@sentry/types';
import { isString } from '@sentry/utils';
import { consoleSandbox, isString } from '@sentry/utils';

import { runOutsideAngular } from './zone';

Expand Down Expand Up @@ -119,7 +119,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
// When in development mode, log the error to console for immediate feedback.
if (this._options.logErrors) {
// eslint-disable-next-line no-console
console.error(extractedError);
consoleSandbox(() => console.error(extractedError));
}

// Optionally show user dialog to provide details on what happened.
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/integrations/local-variables/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Debugger, InspectorNotification, Runtime } from 'node:inspector';
import { Session } from 'node:inspector/promises';
import { workerData } from 'node:worker_threads';
import { consoleSandbox } from '@sentry/utils';
import type { LocalVariablesWorkerArgs, PausedExceptionEvent, RateLimitIncrement, Variables } from './common';
import { LOCAL_VARIABLES_KEY } from './common';
import { createRateLimiter } from './common';
Expand All @@ -10,7 +11,7 @@ const options: LocalVariablesWorkerArgs = workerData;
function log(...args: unknown[]): void {
if (options.debug) {
// eslint-disable-next-line no-console
console.log('[LocalVariables Worker]', ...args);
consoleSandbox(() => console.log('[LocalVariables Worker]', ...args));
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import { consoleSandbox } from '@sentry/utils';
import type { SentryNuxtModuleOptions } from './common/types';
import { addSentryTopImport, addServerConfigToBuild } from './vite/addServerConfig';
import { setupSourceMaps } from './vite/sourceMaps';
Expand Down Expand Up @@ -70,10 +71,12 @@ export default defineNuxtModule<ModuleOptions>({
addSentryTopImport(moduleOptions, nuxt);
} else {
if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`,
);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Using your \`${serverConfigFile}\` file for the server-side Sentry configuration. In case you have a \`public/instrument.server\` file, the \`public/instrument.server\` file will be ignored. Make sure the file path in your node \`--import\` option matches the Sentry server config file in your \`.output\` folder and has a \`.mjs\` extension.`,
);
});
}
}
}
Expand Down
45 changes: 27 additions & 18 deletions packages/nuxt/src/vite/addServerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import { createResolver } from '@nuxt/kit';
import type { Nuxt } from '@nuxt/schema';
import { consoleSandbox } from '@sentry/utils';
import type { SentryNuxtModuleOptions } from '../common/types';

/**
Expand Down Expand Up @@ -38,18 +39,22 @@ export function addServerConfigToBuild(
await fs.promises.copyFile(source, destination);

if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Successfully added the content of the \`${serverConfigFile}\` file to \`${destination}\``,
);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Successfully added the content of the \`${serverConfigFile}\` file to \`${destination}\``,
);
});
}
} catch (error) {
if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] An error occurred when trying to add the \`${serverConfigFile}\` file to the \`.output\` directory`,
error,
);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] An error occurred when trying to add the \`${serverConfigFile}\` file to the \`.output\` directory`,
error,
);
});
}
}
});
Expand All @@ -72,20 +77,24 @@ export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nuxt:

fs.writeFile(entryFilePath, updatedContent, 'utf8', () => {
if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Successfully added the Sentry import to the server entry file "\`${entryFilePath}\`"`,
);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Successfully added the Sentry import to the server entry file "\`${entryFilePath}\`"`,
);
});
}
});
});
} catch (err) {
if (moduleOptions.debug) {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] An error occurred when trying to add the Sentry import to the server entry file "\`${entryFilePath}\`":`,
err,
);
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] An error occurred when trying to add the Sentry import to the server entry file "\`${entryFilePath}\`":`,
err,
);
});
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/sveltekit/src/server/handleError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { captureException } from '@sentry/node';
import { consoleSandbox } from '@sentry/utils';
import type { HandleServerError } from '@sveltejs/kit';

import { flushIfServerless } from './utils';
Expand All @@ -8,7 +9,7 @@ import { flushIfServerless } from './utils';
function defaultErrorHandler({ error }: Parameters<HandleServerError>[0]): ReturnType<HandleServerError> {
// @ts-expect-error this conforms to the default implementation (including this ts-expect-error)
// eslint-disable-next-line no-console
console.error(error && error.stack);
consoleSandbox(() => console.error(error && error.stack));
}

type HandleServerErrorInput = Parameters<HandleServerError>[0];
Expand Down

0 comments on commit 5d0094a

Please sign in to comment.