Do not show monaco JS errors (#30862)

Fix #30861
This commit is contained in:
wxiaoguang 2024-05-06 00:34:13 +08:00 committed by GitHub
parent 5c236bd4c0
commit 982b20d259
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,13 +6,20 @@
// This file must be imported before any lazy-loading is being attempted. // This file must be imported before any lazy-loading is being attempted.
__webpack_public_path__ = `${window.config?.assetUrlPrefix ?? '/assets'}/`; __webpack_public_path__ = `${window.config?.assetUrlPrefix ?? '/assets'}/`;
export function showGlobalErrorMessage(msg) { function shouldIgnoreError(err) {
const pageContent = document.querySelector('.page-content'); const ignorePatterns = [
if (!pageContent) return; '/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 export function showGlobalErrorMessage(msg) {
const msgCompact = msg.replace(/\W/g, '').trim(); const msgContainer = document.querySelector('.page-content') ?? document.body;
let msgDiv = pageContent.querySelector(`.js-global-error[data-global-error-msg-compact="${msgCompact}"]`); 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) { if (!msgDiv) {
const el = document.createElement('div'); const el = document.createElement('div');
el.innerHTML = `<div class="ui container negative message center aligned js-global-error tw-mt-[15px] tw-whitespace-pre-line"></div>`; el.innerHTML = `<div class="ui container negative message center aligned js-global-error tw-mt-[15px] tw-whitespace-pre-line"></div>`;
@ -23,7 +30,7 @@ export function showGlobalErrorMessage(msg) {
msgDiv.setAttribute(`data-global-error-msg-compact`, msgCompact); msgDiv.setAttribute(`data-global-error-msg-compact`, msgCompact);
msgDiv.setAttribute(`data-global-error-msg-count`, msgCount.toString()); msgDiv.setAttribute(`data-global-error-msg-count`, msgCount.toString());
msgDiv.textContent = msg + (msgCount > 1 ? ` (${msgCount})` : ''); 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 (runModeIsProd) return;
} }
// If the error stack trace does not include the base URL of our script assets, it likely came if (err instanceof Error) {
// from a browser extension or inline script. Do not show such errors in production. // If the error stack trace does not include the base URL of our script assets, it likely came
if (err instanceof Error && !err.stack?.includes(assetBaseUrl) && runModeIsProd) { // from a browser extension or inline script. Do not show such errors in production.
return; 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; let msg = err?.message ?? message;