main
Raw Download raw file
 1/*
 2 * noVNC: HTML5 VNC client
 3 * Copyright (C) 2019 The noVNC authors
 4 * Licensed under MPL 2.0 (see LICENSE.txt)
 5 *
 6 * See README.md for usage and integration instructions.
 7 */
 8
 9// Fallback for all uncaught errors
10function handleError(event, err) {
11    try {
12        const msg = document.getElementById('noVNC_fallback_errormsg');
13
14        // Work around Firefox bug:
15        // https://bugzilla.mozilla.org/show_bug.cgi?id=1685038
16        if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
17            return false;
18        }
19
20        // Only show the initial error
21        if (msg.hasChildNodes()) {
22            return false;
23        }
24
25        let div = document.createElement("div");
26        div.classList.add('noVNC_message');
27        div.appendChild(document.createTextNode(event.message));
28        msg.appendChild(div);
29
30        if (event.filename) {
31            div = document.createElement("div");
32            div.className = 'noVNC_location';
33            let text = event.filename;
34            if (event.lineno !== undefined) {
35                text += ":" + event.lineno;
36                if (event.colno !== undefined) {
37                    text += ":" + event.colno;
38                }
39            }
40            div.appendChild(document.createTextNode(text));
41            msg.appendChild(div);
42        }
43
44        if (err && err.stack) {
45            div = document.createElement("div");
46            div.className = 'noVNC_stack';
47            div.appendChild(document.createTextNode(err.stack));
48            msg.appendChild(div);
49        }
50
51        document.getElementById('noVNC_fallback_error')
52            .classList.add("noVNC_open");
53
54    } catch (exc) {
55        document.write("noVNC encountered an error.");
56    }
57
58    // Try to disable keyboard interaction, best effort
59    try {
60        // Remove focus from the currently focused element in order to
61        // prevent keyboard interaction from continuing
62        if (document.activeElement) { document.activeElement.blur(); }
63
64        // Don't let any element be focusable when showing the error
65        let keyboardFocusable = 'a[href], button, input, textarea, select, details, [tabindex]';
66        document.querySelectorAll(keyboardFocusable).forEach((elem) => {
67            elem.setAttribute("tabindex", "-1");
68        });
69    } catch (exc) {
70        // Do nothing
71    }
72
73    // Don't return true since this would prevent the error
74    // from being printed to the browser console.
75    return false;
76}
77
78window.addEventListener('error', evt => handleError(evt, evt.error));
79window.addEventListener('unhandledrejection', evt => handleError(evt.reason, evt.reason));