Skip to content

Commit

Permalink
fix(rrweb): exteranl function errors should be tagged
Browse files Browse the repository at this point in the history
  • Loading branch information
jackson-amplitude committed Jun 6, 2024
1 parent ba5958b commit 510bd8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/rrweb/src/record/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ErrorHandler } from '../types';

type Callback = (...args: unknown[]) => unknown;
type GenericFunction = (...args: any[]) => any;
type ExternalError = Error & {_external_: boolean}

let errorHandler: ErrorHandler | undefined;

Expand Down Expand Up @@ -34,3 +36,18 @@ export const callbackWrapper = <T extends Callback>(cb: T): T => {

return rrwebWrapped;
};

export function externalFunctionWrapper<T extends GenericFunction>(func: T): (...args: Parameters<T>) => ReturnType<T> {
return (...args: Parameters<T>) => {
try {
return func(...args);
} catch (error) {
try {
(error as ExternalError)._external_ = true;
} catch {
// in case we can't assign, don't do anything.
}
throw error
};
}
}
8 changes: 4 additions & 4 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
patch,
throttle,
} from '../utils';
import { callbackWrapper } from './error-handler';
import { callbackWrapper, externalFunctionWrapper } from './error-handler';
import MutationBuffer from './mutation';

type WindowWithStoredMutationObserver = IWindow & {
Expand Down Expand Up @@ -665,7 +665,7 @@ function initStyleSheetObserver(
adds: [{ rule, index }],
});
}
return target.apply(thisArg, argumentsList);
return externalFunctionWrapper(target.apply)(thisArg, argumentsList);
},
),
});
Expand Down Expand Up @@ -694,7 +694,7 @@ function initStyleSheetObserver(
removes: [{ index }],
});
}
return target.apply(thisArg, argumentsList);
return externalFunctionWrapper(target.apply)(thisArg, argumentsList);
},
),
});
Expand Down Expand Up @@ -726,7 +726,7 @@ function initStyleSheetObserver(
replace: text,
});
}
return target.apply(thisArg, argumentsList);
return externalFunctionWrapper(target.apply)(thisArg, argumentsList);
},
),
});
Expand Down

0 comments on commit 510bd8f

Please sign in to comment.