您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GlobalOnErrorHandler.js 2.3KB

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