From 982b20d259e5bcd94377820ca3559112add36a1b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 6 May 2024 00:34:13 +0800 Subject: [PATCH] Do not show monaco JS errors (#30862) Fix #30861 --- web_src/js/bootstrap.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index e466d0b169..8339b4bd82 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -6,13 +6,20 @@ // This file must be imported before any lazy-loading is being attempted. __webpack_public_path__ = `${window.config?.assetUrlPrefix ?? '/assets'}/`; -export function showGlobalErrorMessage(msg) { - const pageContent = document.querySelector('.page-content'); - if (!pageContent) return; +function shouldIgnoreError(err) { + const ignorePatterns = [ + '/assets/js/monaco.', // https://github.com/go-gitea/gitea/issues/30861 , https://github.com/microsoft/monaco-editor/issues/4496 + ]; + for (const pattern of ignorePatterns) { + if (err.stack?.includes(pattern)) return true; + } + return false; +} - // compact the message to a data attribute to avoid too many duplicated messages - const msgCompact = msg.replace(/\W/g, '').trim(); - let msgDiv = pageContent.querySelector(`.js-global-error[data-global-error-msg-compact="${msgCompact}"]`); +export function showGlobalErrorMessage(msg) { + const msgContainer = document.querySelector('.page-content') ?? document.body; + const msgCompact = msg.replace(/\W/g, '').trim(); // compact the message to a data attribute to avoid too many duplicated messages + let msgDiv = msgContainer.querySelector(`.js-global-error[data-global-error-msg-compact="${msgCompact}"]`); if (!msgDiv) { const el = document.createElement('div'); el.innerHTML = `
`; @@ -23,7 +30,7 @@ export function showGlobalErrorMessage(msg) { msgDiv.setAttribute(`data-global-error-msg-compact`, msgCompact); msgDiv.setAttribute(`data-global-error-msg-count`, msgCount.toString()); msgDiv.textContent = msg + (msgCount > 1 ? ` (${msgCount})` : ''); - pageContent.prepend(msgDiv); + msgContainer.prepend(msgDiv); } /** @@ -52,10 +59,12 @@ function processWindowErrorEvent({error, reason, message, type, filename, lineno if (runModeIsProd) return; } - // If the error stack trace does not include the base URL of our script assets, it likely came - // from a browser extension or inline script. Do not show such errors in production. - if (err instanceof Error && !err.stack?.includes(assetBaseUrl) && runModeIsProd) { - return; + if (err instanceof Error) { + // If the error stack trace does not include the base URL of our script assets, it likely came + // from a browser extension or inline script. Do not show such errors in production. + if (!err.stack?.includes(assetBaseUrl) && runModeIsProd) return; + // Ignore some known errors that are unable to fix + if (shouldIgnoreError(err)) return; } let msg = err?.message ?? message;