diff --git a/src/main.tsx b/src/main.tsx index 6680f8812..f48a4a183 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -26,6 +26,10 @@ import { UpdateStateProvider, } from "./services/states"; import { disableWebViewShortcuts } from "./utils/disable-webview-shortcuts"; +import { + isIgnoredMonacoWorkerError, + patchMonacoWorkerConsole, +} from "./utils/monaco-worker-ignore"; if (!window.ResizeObserver) { window.ResizeObserver = ResizeObserver; @@ -87,12 +91,22 @@ bootstrap().catch((error) => { }); }); +patchMonacoWorkerConsole(); + // Error handling window.addEventListener("error", (event) => { + if (isIgnoredMonacoWorkerError(event.error ?? event.message)) { + event.preventDefault(); + return; + } console.error("[main.tsx] Global error:", event.error); }); window.addEventListener("unhandledrejection", (event) => { + if (isIgnoredMonacoWorkerError(event.reason)) { + event.preventDefault(); + return; + } console.error("[main.tsx] Unhandled promise rejection:", event.reason); }); diff --git a/src/utils/monaco-worker-ignore.ts b/src/utils/monaco-worker-ignore.ts new file mode 100644 index 000000000..980158d17 --- /dev/null +++ b/src/utils/monaco-worker-ignore.ts @@ -0,0 +1,38 @@ +// These warnings are safe to ignore: they occur when Monaco models or workers are manually disposed. +const ignoredGlobalErrorMessages = [ + "Missing requestHandler or method:", + "Could not create web worker(s). Falling back to loading web worker code in main thread", + "Cannot use 'in' operator to search for 'then' in undefined", +]; + +const isIgnoredMessage = (message: string) => + ignoredGlobalErrorMessages.some((snippet) => message.includes(snippet)); + +export const isIgnoredMonacoWorkerError = (reason: unknown) => { + const message = String( + reason instanceof Error ? reason.message : (reason ?? ""), + ); + return isIgnoredMessage(message); +}; + +const shouldIgnoreConsoleArgs = (args: unknown[]) => + args.some((arg) => { + const message = + typeof arg === "string" ? arg : arg instanceof Error ? arg.message : ""; + return isIgnoredMessage(message); + }); + +export const patchMonacoWorkerConsole = () => { + const originalWarn = console.warn; + const originalError = console.error; + + console.warn = (...args) => { + if (shouldIgnoreConsoleArgs(args)) return; + originalWarn(...args); + }; + + console.error = (...args) => { + if (shouldIgnoreConsoleArgs(args)) return; + originalError(...args); + }; +};