Convert error handler to ES6 module

We no longer support older browsers, so this is not allowed to use
modern features.
This commit is contained in:
Pierre Ossman 2021-11-22 14:03:19 +01:00
parent 2f602da961
commit c143a852b1
2 changed files with 52 additions and 62 deletions

View File

@ -6,67 +6,57 @@
* See README.md for usage and integration instructions. * See README.md for usage and integration instructions.
*/ */
// NB: this should *not* be included as a module until we have // Fallback for all uncought errors
// native support in the browsers, so that our error handler function handleError(event, err) {
// can catch script-loading errors. try {
const msg = document.getElementById('noVNC_fallback_errormsg');
// No ES6 can be used in this file since it's used for the translation // Work around Firefox bug:
/* eslint-disable prefer-arrow-callback */ // https://bugzilla.mozilla.org/show_bug.cgi?id=1685038
if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
(function _scope() { return false;
"use strict";
// Fallback for all uncought errors
function handleError(event, err) {
try {
const msg = document.getElementById('noVNC_fallback_errormsg');
// Work around Firefox bug:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1685038
if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
return false;
}
// Only show the initial error
if (msg.hasChildNodes()) {
return false;
}
let div = document.createElement("div");
div.classList.add('noVNC_message');
div.appendChild(document.createTextNode(event.message));
msg.appendChild(div);
if (event.filename) {
div = document.createElement("div");
div.className = 'noVNC_location';
let text = event.filename;
if (event.lineno !== undefined) {
text += ":" + event.lineno;
if (event.colno !== undefined) {
text += ":" + event.colno;
}
}
div.appendChild(document.createTextNode(text));
msg.appendChild(div);
}
if (err && err.stack) {
div = document.createElement("div");
div.className = 'noVNC_stack';
div.appendChild(document.createTextNode(err.stack));
msg.appendChild(div);
}
document.getElementById('noVNC_fallback_error')
.classList.add("noVNC_open");
} catch (exc) {
document.write("noVNC encountered an error.");
} }
// Don't return true since this would prevent the error
// from being printed to the browser console. // Only show the initial error
return false; if (msg.hasChildNodes()) {
return false;
}
let div = document.createElement("div");
div.classList.add('noVNC_message');
div.appendChild(document.createTextNode(event.message));
msg.appendChild(div);
if (event.filename) {
div = document.createElement("div");
div.className = 'noVNC_location';
let text = event.filename;
if (event.lineno !== undefined) {
text += ":" + event.lineno;
if (event.colno !== undefined) {
text += ":" + event.colno;
}
}
div.appendChild(document.createTextNode(text));
msg.appendChild(div);
}
if (err && err.stack) {
div = document.createElement("div");
div.className = 'noVNC_stack';
div.appendChild(document.createTextNode(err.stack));
msg.appendChild(div);
}
document.getElementById('noVNC_fallback_error')
.classList.add("noVNC_open");
} catch (exc) {
document.write("noVNC encountered an error.");
} }
window.addEventListener('error', function onerror(evt) { handleError(evt, evt.error); }); // Don't return true since this would prevent the error
window.addEventListener('unhandledrejection', function onreject(evt) { handleError(evt.reason, evt.reason); }); // from being printed to the browser console.
})(); return false;
}
window.addEventListener('error', evt => handleError(evt, evt.error));
window.addEventListener('unhandledrejection', evt => handleError(evt.reason, evt.reason));

View File

@ -55,7 +55,7 @@
<link rel="preload" as="image" href="app/images/error.svg"> <link rel="preload" as="image" href="app/images/error.svg">
<link rel="preload" as="image" href="app/images/warning.svg"> <link rel="preload" as="image" href="app/images/warning.svg">
<script src="app/error-handler.js"></script> <script type="module" crossorigin="anonymous" src="app/error-handler.js"></script>
<script type="module" crossorigin="anonymous" src="app/ui.js"></script> <script type="module" crossorigin="anonymous" src="app/ui.js"></script>
</head> </head>