From 5d0094a1d015566b11c408331418ae84c9b3a361 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 13 Sep 2024 14:06:34 +0200 Subject: [PATCH] fix: Ensure all logs are wrapped with `consoleSandbox` (#13690) To avoid infinite loops etc. --- packages/angular/src/errorhandler.ts | 4 +- .../integrations/local-variables/worker.ts | 3 +- packages/nuxt/src/module.ts | 11 +++-- packages/nuxt/src/vite/addServerConfig.ts | 45 +++++++++++-------- packages/sveltekit/src/server/handleError.ts | 3 +- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/packages/angular/src/errorhandler.ts b/packages/angular/src/errorhandler.ts index 14ca380ea3ea..f1771ba81b7e 100644 --- a/packages/angular/src/errorhandler.ts +++ b/packages/angular/src/errorhandler.ts @@ -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'; @@ -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. diff --git a/packages/node/src/integrations/local-variables/worker.ts b/packages/node/src/integrations/local-variables/worker.ts index f0b3e20ce9b2..77299c0aff29 100644 --- a/packages/node/src/integrations/local-variables/worker.ts +++ b/packages/node/src/integrations/local-variables/worker.ts @@ -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'; @@ -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)); } } diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 0afdeaa03de7..f43f30d7e5ee 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -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'; @@ -70,10 +71,12 @@ export default defineNuxtModule({ 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.`, + ); + }); } } } diff --git a/packages/nuxt/src/vite/addServerConfig.ts b/packages/nuxt/src/vite/addServerConfig.ts index dee15ee34dce..60f2452cde5b 100644 --- a/packages/nuxt/src/vite/addServerConfig.ts +++ b/packages/nuxt/src/vite/addServerConfig.ts @@ -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'; /** @@ -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, + ); + }); } } }); @@ -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, + ); + }); } } }); diff --git a/packages/sveltekit/src/server/handleError.ts b/packages/sveltekit/src/server/handleError.ts index 1289e76a5ee2..f61251245c4d 100644 --- a/packages/sveltekit/src/server/handleError.ts +++ b/packages/sveltekit/src/server/handleError.ts @@ -1,4 +1,5 @@ import { captureException } from '@sentry/node'; +import { consoleSandbox } from '@sentry/utils'; import type { HandleServerError } from '@sveltejs/kit'; import { flushIfServerless } from './utils'; @@ -8,7 +9,7 @@ import { flushIfServerless } from './utils'; function defaultErrorHandler({ error }: Parameters[0]): ReturnType { // @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[0];