選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GlobalOnErrorHandler.js 2.3KB

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