Skip to content

Commit

Permalink
feat: Allow to pass errorHandler as record option
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Feb 3, 2023
1 parent fc82869 commit 66ddc3b
Show file tree
Hide file tree
Showing 5 changed files with 886 additions and 293 deletions.
37 changes: 37 additions & 0 deletions packages/rrweb/src/record/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ErrorHandler } from '../types';

type Callback = (...args: unknown[]) => unknown;

let errorHandler: ErrorHandler | undefined;

export function registerErrorHandler(handler: ErrorHandler | undefined) {
errorHandler = handler;
}

export function unregisterErrorHandler() {
errorHandler = undefined;
}

/**
* Wrap callbacks in a wrapper that allows to pass errors to a configured `errorHandler` method.
*/

export const callbackWrapper = <T extends Callback>(cb: T): T => {
if (!errorHandler) {
return cb;
}

const rrwebWrapped = (((...rest: unknown[]) => {
try {
return cb(...rest);
} catch (error) {
if (errorHandler && errorHandler(error) === true) {
return;
}

throw error;
}
}) as unknown) as T;

return rrwebWrapped;
};
7 changes: 6 additions & 1 deletion packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { IframeManager } from './iframe-manager';
import { ShadowDomManager } from './shadow-dom-manager';
import { CanvasManager } from './observers/canvas/canvas-manager';
import { StylesheetManager } from './stylesheet-manager';
import { callbackWrapper, registerErrorHandler, unregisterErrorHandler } from './errorHandler';

function wrapEvent(e: event): eventWithTime {
return {
Expand Down Expand Up @@ -82,8 +83,11 @@ function record<T = eventWithTime>(
plugins,
keepIframeSrcFn = () => false,
ignoreCSSAttributes = new Set([]),
errorHandler,
} = options;

registerErrorHandler(errorHandler);

const inEmittingFrame = recordCrossOriginIframes
? window.parent === window
: true;
Expand Down Expand Up @@ -420,7 +424,7 @@ function record<T = eventWithTime>(
);

const observe = (doc: Document) => {
return initObservers(
return callbackWrapper(initObservers)(
{
mutationCb: wrappedMutationEmit,
mousemoveCb: (positions, source) =>
Expand Down Expand Up @@ -602,6 +606,7 @@ function record<T = eventWithTime>(
return () => {
handlers.forEach((h) => h());
recording = false;
unregisterErrorHandler();
};
} catch (error) {
// TODO: handle internal error
Expand Down
Loading

0 comments on commit 66ddc3b

Please sign in to comment.