MeshCentral/public/novnc/app/error-handler.js

80 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-11-29 15:08:15 -05:00
/*
* noVNC: HTML5 VNC client
* Copyright (C) 2019 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
2023-07-05 06:20:10 -04:00
// Fallback for all uncought errors
function handleError(event, err) {
try {
const msg = document.getElementById('noVNC_fallback_errormsg');
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
// Work around Firefox bug:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1685038
if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
return false;
}
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
// Only show the initial error
if (msg.hasChildNodes()) {
return false;
}
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
let div = document.createElement("div");
div.classList.add('noVNC_message');
div.appendChild(document.createTextNode(event.message));
msg.appendChild(div);
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
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;
}
2020-06-06 21:55:47 -04:00
}
2023-07-05 06:20:10 -04:00
div.appendChild(document.createTextNode(text));
msg.appendChild(div);
}
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
if (err && err.stack) {
div = document.createElement("div");
div.className = 'noVNC_stack';
div.appendChild(document.createTextNode(err.stack));
2020-06-06 21:55:47 -04:00
msg.appendChild(div);
2023-07-05 06:20:10 -04:00
}
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
document.getElementById('noVNC_fallback_error')
.classList.add("noVNC_open");
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
} catch (exc) {
document.write("noVNC encountered an error.");
}
2020-06-06 21:55:47 -04:00
2023-07-05 06:20:10 -04:00
// Try to disable keyboard interaction, best effort
try {
// Remove focus from the currently focused element in order to
// prevent keyboard interaction from continuing
if (document.activeElement) { document.activeElement.blur(); }
// Don't let any element be focusable when showing the error
let keyboardFocusable = 'a[href], button, input, textarea, select, details, [tabindex]';
document.querySelectorAll(keyboardFocusable).forEach((elem) => {
elem.setAttribute("tabindex", "-1");
});
} catch (exc) {
// Do nothing
2020-06-06 21:55:47 -04:00
}
2023-07-05 06:20:10 -04:00
// Don't return true since this would prevent the error
// 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));