errorEventHandlers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @callback EventCallback
  3. * @param {string | Error | null} context
  4. * @returns {void}
  5. */
  6. /**
  7. * @callback EventHandler
  8. * @param {Event} event
  9. * @returns {void}
  10. */
  11. /**
  12. * A function that creates an event handler for the `error` event.
  13. * @param {EventCallback} callback A function called to handle the error context.
  14. * @returns {EventHandler} A handler for the `error` event.
  15. */
  16. function createErrorHandler(callback) {
  17. return function errorHandler(event) {
  18. if (!event || !event.error) {
  19. return callback(null);
  20. }
  21. if (event.error instanceof Error) {
  22. return callback(event.error);
  23. }
  24. // A non-error was thrown, we don't have a trace. :(
  25. // Look in your browser's devtools for more information
  26. return callback(new Error(event.error));
  27. };
  28. }
  29. /**
  30. * A function that creates an event handler for the `unhandledrejection` event.
  31. * @param {EventCallback} callback A function called to handle the error context.
  32. * @returns {EventHandler} A handler for the `unhandledrejection` event.
  33. */
  34. function createRejectionHandler(callback) {
  35. return function rejectionHandler(event) {
  36. if (!event || !event.reason) {
  37. return callback(new Error('Unknown'));
  38. }
  39. if (event.reason instanceof Error) {
  40. return callback(event.reason);
  41. }
  42. // A non-error was rejected, we don't have a trace :(
  43. // Look in your browser's devtools for more information
  44. return callback(new Error(event.reason));
  45. };
  46. }
  47. /**
  48. * Creates a handler that registers an EventListener on window for a valid type
  49. * and calls a callback when the event fires.
  50. * @param {string} eventType A valid DOM event type.
  51. * @param {function(EventCallback): EventHandler} createHandler A function that creates an event handler.
  52. * @returns {register} A function that registers the EventListener given a callback.
  53. */
  54. function createWindowEventHandler(eventType, createHandler) {
  55. /**
  56. * @type {EventHandler | null} A cached event handler function.
  57. */
  58. let eventHandler = null;
  59. /**
  60. * Unregisters an EventListener if it has been registered.
  61. * @returns {void}
  62. */
  63. function unregister() {
  64. if (eventHandler === null) {
  65. return;
  66. }
  67. window.removeEventListener(eventType, eventHandler);
  68. eventHandler = null;
  69. }
  70. /**
  71. * Registers an EventListener if it hasn't been registered.
  72. * @param {EventCallback} callback A function called after the event handler to handle its context.
  73. * @returns {unregister | void} A function to unregister the registered EventListener if registration is performed.
  74. */
  75. function register(callback) {
  76. if (eventHandler !== null) {
  77. return;
  78. }
  79. eventHandler = createHandler(callback);
  80. window.addEventListener(eventType, eventHandler);
  81. return unregister;
  82. }
  83. return register;
  84. }
  85. module.exports = {
  86. error: createWindowEventHandler('error', createErrorHandler),
  87. unhandledRejection: createWindowEventHandler('unhandledrejection', createRejectionHandler),
  88. };