From c79619f2274fcd2874e22f9bceaecfdac6acf91f Mon Sep 17 00:00:00 2001
From: Billy Vong <billyvg@users.noreply.github.com>
Date: Wed, 31 Jan 2024 12:35:29 -0330
Subject: [PATCH] feat: Register `errorHandler` inside of CanvasManager (#161)

Needed as we (Sentry), codesplit CanvasManager. Also fixes uncaught
exceptions from `createImageBitmap`

---------

Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
---
 packages/rrweb/src/record/index.ts                    |  1 +
 .../src/record/observers/canvas/canvas-manager.ts     | 11 +++++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/packages/rrweb/src/record/index.ts b/packages/rrweb/src/record/index.ts
index 10b2aa594c..0945f585d7 100644
--- a/packages/rrweb/src/record/index.ts
+++ b/packages/rrweb/src/record/index.ts
@@ -358,6 +358,7 @@ function record<T = eventWithTime>(
       unblockSelector,
       sampling: sampling['canvas'],
       dataURLOptions,
+      errorHandler,
     },
   );
 
diff --git a/packages/rrweb/src/record/observers/canvas/canvas-manager.ts b/packages/rrweb/src/record/observers/canvas/canvas-manager.ts
index 276b1e9c00..c6e29a2e47 100644
--- a/packages/rrweb/src/record/observers/canvas/canvas-manager.ts
+++ b/packages/rrweb/src/record/observers/canvas/canvas-manager.ts
@@ -20,7 +20,8 @@ import initCanvas2DMutationObserver from './2d';
 import initCanvasContextObserver from './canvas';
 import initCanvasWebGLMutationObserver from './webgl';
 import { getImageBitmapDataUrlWorkerURL } from '@sentry-internal/rrweb-worker';
-import { callbackWrapper } from '../../error-handler';
+import { callbackWrapper, registerErrorHandler } from '../../error-handler';
+import type { ErrorHandler } from '../../../types';
 
 export type RafStamps = { latestId: number; invokeId: number | null };
 
@@ -47,8 +48,9 @@ export interface CanvasManagerConstructorOptions {
   blockSelector: string | null;
   unblockSelector: string | null;
   mirror: Mirror;
-  sampling?: 'all' | number;
   dataURLOptions: DataURLOptions;
+  errorHandler?: ErrorHandler;
+  sampling?: 'all' | number;
 }
 
 export class CanvasManagerNoop implements CanvasManagerInterface {
@@ -113,11 +115,16 @@ export class CanvasManager implements CanvasManagerInterface {
       unblockSelector,
       recordCanvas,
       dataURLOptions,
+      errorHandler,
     } = options;
     this.mutationCb = options.mutationCb;
     this.mirror = options.mirror;
     this.options = options;
 
+    if (errorHandler) {
+      registerErrorHandler(errorHandler);
+    }
+
     if (options.enableManualSnapshot) {
       return;
     }