From e3823803aadce7902d3bd68a75ca20f662b69ee5 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 1 Dec 2017 20:04:50 +0200 Subject: [PATCH 1/2] Handle javascript errors while loading --- client/index.html | 2 +- client/js/loading-slow-alert.js | 15 +++++++++++++++ client/js/socket-events/init.js | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/client/index.html b/client/index.html index 78051d25..e51999d7 100644 --- a/client/index.html +++ b/client/index.html @@ -48,7 +48,7 @@
-

The Lounge is loading…

+

The Lounge is loading…

Loading the app… Make sure to have JavaScript enabled.

diff --git a/client/js/loading-slow-alert.js b/client/js/loading-slow-alert.js index debff175..1029004e 100644 --- a/client/js/loading-slow-alert.js +++ b/client/js/loading-slow-alert.js @@ -19,3 +19,18 @@ setTimeout(function() { document.getElementById("loading-slow-reload").addEventListener("click", function() { location.reload(); }); + +window.g_LoungeErrorHandler = function LoungeErrorHandler(error) { + var title = document.getElementById("loading-title"); + title.textContent = "An error has occured"; + + title = document.getElementById("loading-page-message"); + title.textContent = "An error has occured that prevented the client from loading correctly."; + + var element = document.createElement("p"); + element.contentEditable = true; + element.textContent = error instanceof ErrorEvent ? error.message : error; + title.parentNode.insertBefore(element, title.nextSibling); +}; + +window.addEventListener("error", window.g_LoungeErrorHandler); diff --git a/client/js/socket-events/init.js b/client/js/socket-events/init.js index 19e82ef3..7dae4c94 100644 --- a/client/js/socket-events/init.js +++ b/client/js/socket-events/init.js @@ -44,6 +44,11 @@ socket.on("init", function(data) { $("body").removeClass("signed-out"); $("#loading").remove(); $("#sign-in").remove(); + + if (window.g_LoungeErrorHandler) { + window.removeEventListener("error", window.g_LoungeErrorHandler); + window.g_LoungeErrorHandler = null; + } } openCorrectChannel(previousActive, data.active); From 2afd4d7ec4ce7d04cc88d3411a489846d7861326 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 5 Dec 2017 15:17:31 +0200 Subject: [PATCH 2/2] Hide error under details --- client/js/loading-slow-alert.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/client/js/loading-slow-alert.js b/client/js/loading-slow-alert.js index 1029004e..2eb990dd 100644 --- a/client/js/loading-slow-alert.js +++ b/client/js/loading-slow-alert.js @@ -27,10 +27,21 @@ window.g_LoungeErrorHandler = function LoungeErrorHandler(error) { title = document.getElementById("loading-page-message"); title.textContent = "An error has occured that prevented the client from loading correctly."; - var element = document.createElement("p"); - element.contentEditable = true; - element.textContent = error instanceof ErrorEvent ? error.message : error; - title.parentNode.insertBefore(element, title.nextSibling); + var summary = document.createElement("summary"); + summary.textContent = "More details"; + + if (error instanceof ErrorEvent) { + error = error.message + "\n\n" + error.stack + "\n\nView developer tools console for more information and a better stacktrace."; + } + + var data = document.createElement("pre"); + data.contentEditable = true; + data.textContent = error; + + var details = document.createElement("details"); + details.appendChild(summary); + details.appendChild(data); + title.parentNode.insertBefore(details, title.nextSibling); }; window.addEventListener("error", window.g_LoungeErrorHandler);