Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GlobalOnErrorHandler.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. oldOnUnhandledRejection && oldOnUnhandledRejection(event);
  30. }
  31. // Setting the custom error handlers.
  32. window.onerror = JitsiGlobalErrorHandler;
  33. window.onunhandledrejection = JitsiGlobalUnhandledRejection;
  34. const GlobalOnErrorHandler = {
  35. /**
  36. * Adds new error handlers.
  37. * @param handler the new handler.
  38. */
  39. addHandler(handler) {
  40. handlers.push(handler);
  41. },
  42. /**
  43. * Calls the global error handler if there is one.
  44. * @param error the error to pass to the error handler
  45. */
  46. callErrorHandler(error) {
  47. const errHandler = window.onerror;
  48. if (!errHandler) {
  49. return;
  50. }
  51. errHandler(null, null, null, null, error);
  52. },
  53. /**
  54. * Calls the global rejection handler if there is one.
  55. * @param error the error to pass to the rejection handler.
  56. */
  57. callUnhandledRejectionHandler(error) {
  58. const errHandler = window.onunhandledrejection;
  59. if (!errHandler) {
  60. return;
  61. }
  62. errHandler(error);
  63. }
  64. };
  65. module.exports = GlobalOnErrorHandler;