You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlobalOnErrorHandler.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * This utility class defines custom onerror and onunhandledrejection functions.
  3. * The custom error handlers respect the previously-defined error handlers.
  4. * GlobalOnErrorHandler class provides utilities to add many custom error
  5. * handlers and to execute the error handlers directly.
  6. */
  7. /**
  8. * List with global error handlers that will be executed.
  9. */
  10. var handlers = [];
  11. // If an old handler exists, also fire its events.
  12. var oldOnErrorHandler = window.onerror;
  13. /**
  14. * Custom error handler that calls the old global error handler and executes
  15. * all handlers that were previously added.
  16. */
  17. function JitsiGlobalErrorHandler(message, source, lineno, colno, error) {
  18. handlers.forEach(function (handler) {
  19. handler(message, source, lineno, colno, error);
  20. });
  21. if (oldOnErrorHandler)
  22. oldOnErrorHandler(message, source, lineno, colno, error);
  23. }
  24. // If an old handler exists, also fire its events.
  25. var oldOnUnhandledRejection = window.onunhandledrejection;
  26. /**
  27. * Custom handler that calls the old global handler and executes all handlers
  28. * that were previously added. This handler handles rejected Promises.
  29. */
  30. function JitsiGlobalUnhandledRejection(event) {
  31. handlers.forEach(function (handler) {
  32. handler(null, null, null, null, event.reason);
  33. });
  34. if(oldOnUnhandledRejection)
  35. oldOnUnhandledRejection(event);
  36. }
  37. // Setting the custom error handlers.
  38. window.onerror = JitsiGlobalErrorHandler;
  39. window.onunhandledrejection = JitsiGlobalUnhandledRejection;
  40. var GlobalOnErrorHandler = {
  41. /**
  42. * Adds new error handlers.
  43. * @param handler the new handler.
  44. */
  45. addHandler: function (handler) {
  46. handlers.push(handler);
  47. },
  48. /**
  49. * Calls the global error handler if there is one.
  50. * @param error the error to pass to the error handler
  51. */
  52. callErrorHandler: function (error) {
  53. var errHandler = window.onerror;
  54. if(!errHandler)
  55. return;
  56. errHandler.call(null, null, null, null, null, error);
  57. },
  58. /**
  59. * Calls the global rejection handler if there is one.
  60. * @param error the error to pass to the rejection handler.
  61. */
  62. callUnhandledRejectionHandler: function (error) {
  63. var errHandler = window.onunhandledrejection;
  64. if(!errHandler)
  65. return;
  66. errHandler.call(null, error);
  67. }
  68. };
  69. module.exports = GlobalOnErrorHandler;