]> git.proxmox.com Git - mirror_novnc.git/blame - app/error-handler.js
Prefer const/let over var
[mirror_novnc.git] / app / error-handler.js
CommitLineData
adfc9d3f
SR
1// NB: this should *not* be included as a module until we have
2// native support in the browsers, so that our error handler
3// can catch script-loading errors.
4
adfc9d3f
SR
5(function(){
6 "use strict";
7
adfc9d3f
SR
8 // Fallback for all uncought errors
9 function handleError (event, err) {
10 try {
2b5f94fa 11 const msg = document.getElementById('noVNC_fallback_errormsg');
adfc9d3f
SR
12
13 // Only show the initial error
14 if (msg.hasChildNodes()) {
15 return false;
16 }
17
2b5f94fa 18 let div = document.createElement("div");
adfc9d3f 19 div.classList.add('noVNC_message');
c361080b 20 div.appendChild(document.createTextNode(event.message));
adfc9d3f
SR
21 msg.appendChild(div);
22
732233ed 23 if (event.filename) {
adfc9d3f
SR
24 div = document.createElement("div");
25 div.className = 'noVNC_location';
2b5f94fa 26 let text = event.filename;
732233ed
PO
27 if (event.lineno !== undefined) {
28 text += ":" + event.lineno;
29 if (event.colno !== undefined) {
30 text += ":" + event.colno;
31 }
32 }
33 div.appendChild(document.createTextNode(text));
adfc9d3f
SR
34 msg.appendChild(div);
35 }
36
7d60e97c 37 if (err && err.stack) {
adfc9d3f
SR
38 div = document.createElement("div");
39 div.className = 'noVNC_stack';
40 div.appendChild(document.createTextNode(err.stack));
41 msg.appendChild(div);
42 }
43
44 document.getElementById('noVNC_fallback_error')
45 .classList.add("noVNC_open");
46 } catch (exc) {
47 document.write("noVNC encountered an error.");
48 }
49 // Don't return true since this would prevent the error
50 // from being printed to the browser console.
51 return false;
52 }
53 window.addEventListener('error', function (evt) { handleError(evt, evt.error); });
54 window.addEventListener('unhandledrejection', function (evt) { handleError(evt.reason, evt.reason); });
55})();