error-handler.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Fallback for all uncought errors
  9. function handleError(event, err) {
  10. try {
  11. const msg = document.getElementById('noVNC_fallback_errormsg');
  12. // Work around Firefox bug:
  13. // https://bugzilla.mozilla.org/show_bug.cgi?id=1685038
  14. if (event.message === "ResizeObserver loop completed with undelivered notifications.") {
  15. return false;
  16. }
  17. // Only show the initial error
  18. if (msg.hasChildNodes()) {
  19. return false;
  20. }
  21. let div = document.createElement("div");
  22. div.classList.add('noVNC_message');
  23. div.appendChild(document.createTextNode(event.message));
  24. msg.appendChild(div);
  25. if (event.filename) {
  26. div = document.createElement("div");
  27. div.className = 'noVNC_location';
  28. let text = event.filename;
  29. if (event.lineno !== undefined) {
  30. text += ":" + event.lineno;
  31. if (event.colno !== undefined) {
  32. text += ":" + event.colno;
  33. }
  34. }
  35. div.appendChild(document.createTextNode(text));
  36. msg.appendChild(div);
  37. }
  38. if (err && err.stack) {
  39. div = document.createElement("div");
  40. div.className = 'noVNC_stack';
  41. div.appendChild(document.createTextNode(err.stack));
  42. msg.appendChild(div);
  43. }
  44. document.getElementById('noVNC_fallback_error')
  45. .classList.add("noVNC_open");
  46. } catch (exc) {
  47. document.write("noVNC encountered an error.");
  48. }
  49. // Try to disable keyboard interaction, best effort
  50. try {
  51. // Remove focus from the currently focused element in order to
  52. // prevent keyboard interaction from continuing
  53. if (document.activeElement) { document.activeElement.blur(); }
  54. // Don't let any element be focusable when showing the error
  55. let keyboardFocusable = 'a[href], button, input, textarea, select, details, [tabindex]';
  56. document.querySelectorAll(keyboardFocusable).forEach((elem) => {
  57. elem.setAttribute("tabindex", "-1");
  58. });
  59. } catch (exc) {
  60. // Do nothing
  61. }
  62. // Don't return true since this would prevent the error
  63. // from being printed to the browser console.
  64. return false;
  65. }
  66. window.addEventListener('error', evt => handleError(evt, evt.error));
  67. window.addEventListener('unhandledrejection', evt => handleError(evt.reason, evt.reason));