From 37e1d3ffc1f71ff65a2d93835a1b22d42a6d5881 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:16:11 +0100 Subject: [PATCH 01/10] Don't show global JS errors in production --- web_src/js/bootstrap.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index f8d0c0cac0c33..fa8df62df8938 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -29,17 +29,18 @@ export function showGlobalErrorMessage(msg) { * @param {ErrorEvent} e */ function processWindowErrorEvent(e) { - if (e.type === 'unhandledrejection') { - showGlobalErrorMessage(`JavaScript promise rejection: ${e.reason}. Open browser console to see more details.`); - return; - } - if (!e.error && e.lineno === 0 && e.colno === 0 && e.filename === '' && window.navigator.userAgent.includes('FxiOS/')) { - // At the moment, Firefox (iOS) (10x) has an engine bug. See https://github.com/go-gitea/gitea/issues/20240 - // If a script inserts a newly created (and content changed) element into DOM, there will be a nonsense error event reporting: Script error: line 0, col 0. - return; // ignore such nonsense error event + // do not show global errors in production as this causes errors from buggy browser extensions to + // show and we can not control or reliably detect whether an error originates from our code or + // from an extension. + if (process.env.NODE_ENV !== 'production') { + let message; + if (e.type === 'unhandledrejection') { + message = `JavaScript promise rejection: ${e.reason}.`; + } else { + message = `JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}).`; + } + showGlobalErrorMessage(`${message} Open browser console to see more details.`); } - - showGlobalErrorMessage(`JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}). Open browser console to see more details.`); } function initGlobalErrorHandler() { From 77969d914eb8b38770746baa5a63f6bc733f867f Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:34:49 +0100 Subject: [PATCH 02/10] detect own errors --- web_src/js/bootstrap.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index fa8df62df8938..7ea85e33b2756 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -29,18 +29,17 @@ export function showGlobalErrorMessage(msg) { * @param {ErrorEvent} e */ function processWindowErrorEvent(e) { - // do not show global errors in production as this causes errors from buggy browser extensions to - // show and we can not control or reliably detect whether an error originates from our code or - // from an extension. - if (process.env.NODE_ENV !== 'production') { - let message; - if (e.type === 'unhandledrejection') { - message = `JavaScript promise rejection: ${e.reason}.`; - } else { - message = `JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}).`; - } - showGlobalErrorMessage(`${message} Open browser console to see more details.`); + const err = e.error ?? e.reason; + const jsDir = `${window.location.origin}${__webpack_public_path__}js`; + if (!err.stack?.includes(jsDir)) return; // error likely from browser extension + + let message; + if (e.type === 'unhandledrejection') { + message = `JavaScript promise rejection: ${err.message}.`; + } else { + message = `JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}).`; } + showGlobalErrorMessage(`${message} Open browser console to see more details.`); } function initGlobalErrorHandler() { From 02c52e19930655ee6d9987162aa8955d4b2441ff Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:39:36 +0100 Subject: [PATCH 03/10] add back check for env --- web_src/js/bootstrap.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index 7ea85e33b2756..06f23171c6e36 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -31,7 +31,10 @@ export function showGlobalErrorMessage(msg) { function processWindowErrorEvent(e) { const err = e.error ?? e.reason; const jsDir = `${window.location.origin}${__webpack_public_path__}js`; - if (!err.stack?.includes(jsDir)) return; // error likely from browser extension + + // error likely from browser extension or some inline script. Do not show these + // in production builds. + if (!err.stack?.includes(jsDir) && process.env.NODE_ENV === 'production') return; let message; if (e.type === 'unhandledrejection') { From 9ef80c97410a6469a11968482620f822d775ad9e Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:40:58 +0100 Subject: [PATCH 04/10] tweak comment --- web_src/js/bootstrap.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index 06f23171c6e36..2ede424a024b3 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -32,8 +32,7 @@ function processWindowErrorEvent(e) { const err = e.error ?? e.reason; const jsDir = `${window.location.origin}${__webpack_public_path__}js`; - // error likely from browser extension or some inline script. Do not show these - // in production builds. + // error is likely from browser extension or inline scripts. Do not show these in production builds. if (!err.stack?.includes(jsDir) && process.env.NODE_ENV === 'production') return; let message; From d0227c0c1855a001590a5d6fcd5e80d38b5f224c Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:45:05 +0100 Subject: [PATCH 05/10] better url building --- web_src/js/bootstrap.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index 2ede424a024b3..e32f51b53df09 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -30,10 +30,10 @@ export function showGlobalErrorMessage(msg) { */ function processWindowErrorEvent(e) { const err = e.error ?? e.reason; - const jsDir = `${window.location.origin}${__webpack_public_path__}js`; + const jsUrl = String(new URL(`${__webpack_public_path__}js`, window.location.origin)); // error is likely from browser extension or inline scripts. Do not show these in production builds. - if (!err.stack?.includes(jsDir) && process.env.NODE_ENV === 'production') return; + if (!err.stack?.includes(jsUrl) && process.env.NODE_ENV === 'production') return; let message; if (e.type === 'unhandledrejection') { From 75325ffbc93a5baea6fd21009bd12ab154d7b593 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:46:48 +0100 Subject: [PATCH 06/10] include whole asset folder --- web_src/js/bootstrap.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index e32f51b53df09..c56fa073a31f5 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -30,10 +30,10 @@ export function showGlobalErrorMessage(msg) { */ function processWindowErrorEvent(e) { const err = e.error ?? e.reason; - const jsUrl = String(new URL(`${__webpack_public_path__}js`, window.location.origin)); + const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin)); // error is likely from browser extension or inline scripts. Do not show these in production builds. - if (!err.stack?.includes(jsUrl) && process.env.NODE_ENV === 'production') return; + if (!err.stack?.includes(assetBaseUrl) && process.env.NODE_ENV === 'production') return; let message; if (e.type === 'unhandledrejection') { From 7731ad033b613efcc3ea9bc8383af59562cd7827 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:51:07 +0100 Subject: [PATCH 07/10] restore firefox handler --- web_src/js/bootstrap.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index c56fa073a31f5..52bee287b0d38 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -41,6 +41,13 @@ function processWindowErrorEvent(e) { } else { message = `JavaScript error: ${e.message} (${e.filename} @ ${e.lineno}:${e.colno}).`; } + + if (!e.error && e.lineno === 0 && e.colno === 0 && e.filename === '' && window.navigator.userAgent.includes('FxiOS/')) { + // At the moment, Firefox (iOS) (10x) has an engine bug. See https://github.com/go-gitea/gitea/issues/20240 + // If a script inserts a newly created (and content changed) element into DOM, there will be a nonsense error event reporting: Script error: line 0, col 0. + return; // ignore such nonsense error event + } + showGlobalErrorMessage(`${message} Open browser console to see more details.`); } From d8fef304b1fbb5356613c1c5374df84d2da27dc9 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 21 Feb 2024 21:57:54 +0100 Subject: [PATCH 08/10] tweak --- web_src/js/bootstrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index 52bee287b0d38..fa6b6be12e73a 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -32,7 +32,7 @@ function processWindowErrorEvent(e) { const err = e.error ?? e.reason; const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin)); - // error is likely from browser extension or inline scripts. Do not show these in production builds. + // error is likely from browser extension or inline script. Do not show these in production builds. if (!err.stack?.includes(assetBaseUrl) && process.env.NODE_ENV === 'production') return; let message; From 7ff096ee4c3027f8226a92364917b031a63924bb Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 17:42:38 +0100 Subject: [PATCH 09/10] use runModeIsProd --- web_src/js/bootstrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index fa6b6be12e73a..dae7f656ee18a 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -33,7 +33,7 @@ function processWindowErrorEvent(e) { const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin)); // error is likely from browser extension or inline script. Do not show these in production builds. - if (!err.stack?.includes(assetBaseUrl) && process.env.NODE_ENV === 'production') return; + if (!err.stack?.includes(assetBaseUrl) && (window.config?.runModeIsProd ?? true)) return; let message; if (e.type === 'unhandledrejection') { From 10729cefbd8c27487699ae5a4660506767afa791 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 22 Feb 2024 18:10:10 +0100 Subject: [PATCH 10/10] show all error when window.config is not there --- web_src/js/bootstrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/bootstrap.js b/web_src/js/bootstrap.js index dae7f656ee18a..e46c91e5e6d98 100644 --- a/web_src/js/bootstrap.js +++ b/web_src/js/bootstrap.js @@ -33,7 +33,7 @@ function processWindowErrorEvent(e) { const assetBaseUrl = String(new URL(__webpack_public_path__, window.location.origin)); // error is likely from browser extension or inline script. Do not show these in production builds. - if (!err.stack?.includes(assetBaseUrl) && (window.config?.runModeIsProd ?? true)) return; + if (!err.stack?.includes(assetBaseUrl) && window.config?.runModeIsProd) return; let message; if (e.type === 'unhandledrejection') {