error-handler.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // NB: this should *not* be included as a module until we have
  9. // native support in the browsers, so that our error handler
  10. // can catch script-loading errors.
  11. // No ES6 can be used in this file since it's used for the translation
  12. /* eslint-disable prefer-arrow-callback */
  13. (function _scope() {
  14. "use strict";
  15. // Fallback for all uncought errors
  16. function handleError(event, err) {
  17. try {
  18. const msg = document.getElementById('noVNC_fallback_errormsg');
  19. // Only show the initial error
  20. if (msg.hasChildNodes()) {
  21. return false;
  22. }
  23. let div = document.createElement("div");
  24. div.classList.add('noVNC_message');
  25. div.appendChild(document.createTextNode(event.message));
  26. msg.appendChild(div);
  27. if (event.filename) {
  28. div = document.createElement("div");
  29. div.className = 'noVNC_location';
  30. let text = event.filename;
  31. if (event.lineno !== undefined) {
  32. text += ":" + event.lineno;
  33. if (event.colno !== undefined) {
  34. text += ":" + event.colno;
  35. }
  36. }
  37. div.appendChild(document.createTextNode(text));
  38. msg.appendChild(div);
  39. }
  40. if (err && err.stack) {
  41. div = document.createElement("div");
  42. div.className = 'noVNC_stack';
  43. div.appendChild(document.createTextNode(err.stack));
  44. msg.appendChild(div);
  45. }
  46. document.getElementById('noVNC_fallback_error')
  47. .classList.add("noVNC_open");
  48. } catch (exc) {
  49. document.write("noVNC encountered an error.");
  50. }
  51. // Don't return true since this would prevent the error
  52. // from being printed to the browser console.
  53. return false;
  54. }
  55. window.addEventListener('error', function onerror(evt) { handleError(evt, evt.error); });
  56. window.addEventListener('unhandledrejection', function onreject(evt) { handleError(evt.reason, evt.reason); });
  57. })();