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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * This utility class defines custom onerror and onunhandledrejection functions
  3. * The custom error handlers are respecting the previous 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
  28. * all handlers that were previously added. This handler handles rejected
  29. * Promises.
  30. */
  31. function JitsiGlobalUnhandlerRejection(event) {
  32. handlers.forEach(function (handler) {
  33. handler(null, null, null, null, event.reason);
  34. });
  35. if(oldOnUnhandledRejection)
  36. oldOnUnhandledRejection(event);
  37. }
  38. //Setting the custom error handlers.
  39. window.onerror = JitsiGlobalErrorHandler;
  40. window.onunhandledrejection = JitsiGlobalUnhandlerRejection;
  41. var GlobalOnErrorHandler = {
  42. /**
  43. * Adds new error handlers.
  44. * @param handler the new handler.
  45. */
  46. addHandler: function (handler) {
  47. handlers.push(handler);
  48. },
  49. /**
  50. * Calls the global error handler if there is one.
  51. * @param error the error that is going to be passed to the error handler
  52. */
  53. callErrorHandler: function (error) {
  54. var errHandler = window.onerror;
  55. if(!errHandler)
  56. return;
  57. errHandler.call(null, null, null, null, null, error);
  58. },
  59. /**
  60. * Calls the global rejection handler if there is one.
  61. * @param error the error that is going to be passed to the rejection
  62. * handler.
  63. */
  64. callUnhandlerRejectionHandler: function (error) {
  65. var errHandler = window.onunhandledrejection;
  66. if(!errHandler)
  67. return;
  68. errHandler.call(null, error);
  69. }
  70. };
  71. module.exports = GlobalOnErrorHandler;