Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

GlobalOnErrorHandler.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const handlers = [];
  11. // If an old handler exists, also fire its events.
  12. const 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(...args) {
  18. handlers.forEach(handler => handler(...args));
  19. oldOnErrorHandler && oldOnErrorHandler(...args);
  20. }
  21. // If an old handler exists, also fire its events.
  22. const oldOnUnhandledRejection = window.onunhandledrejection;
  23. /**
  24. * Custom handler that calls the old global handler and executes all handlers
  25. * that were previously added. This handler handles rejected Promises.
  26. */
  27. function JitsiGlobalUnhandledRejection(event) {
  28. handlers.forEach(handler => handler(null, null, null, null, event.reason));
  29. if (oldOnUnhandledRejection) {
  30. oldOnUnhandledRejection(event);
  31. }
  32. }
  33. // Setting the custom error handlers.
  34. window.onerror = JitsiGlobalErrorHandler;
  35. window.onunhandledrejection = JitsiGlobalUnhandledRejection;
  36. const GlobalOnErrorHandler = {
  37. /**
  38. * Adds new error handlers.
  39. * @param handler the new handler.
  40. */
  41. addHandler(handler) {
  42. handlers.push(handler);
  43. },
  44. /**
  45. * Calls the global error handler if there is one.
  46. * @param error the error to pass to the error handler
  47. */
  48. callErrorHandler(error) {
  49. const errHandler = window.onerror;
  50. if (!errHandler) {
  51. return;
  52. }
  53. errHandler(null, null, null, null, error);
  54. },
  55. /**
  56. * Calls the global rejection handler if there is one.
  57. * @param error the error to pass to the rejection handler.
  58. */
  59. callUnhandledRejectionHandler(error) {
  60. const errHandler = window.onunhandledrejection;
  61. if (!errHandler) {
  62. return;
  63. }
  64. errHandler(error);
  65. }
  66. };
  67. module.exports = GlobalOnErrorHandler;