From 9c8c5df05c89c8a058c591e043019d3a3957e413 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Thu, 12 Oct 2023 14:04:01 -0400 Subject: [PATCH 1/2] feat(record): Allow `callbackWrapper` to attach a `__source__` property to exceptions Allow `callbackWrapper` to attach an optional `__source__` property to a caught exception. This will allow downstream errorHandlers to decide to re-throw an exception based on the source type. --- packages/rrweb/src/record/error-handler.ts | 9 ++++++++- packages/rrweb/src/record/observer.ts | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/rrweb/src/record/error-handler.ts b/packages/rrweb/src/record/error-handler.ts index cc0511457a..4fd3fcf370 100644 --- a/packages/rrweb/src/record/error-handler.ts +++ b/packages/rrweb/src/record/error-handler.ts @@ -15,7 +15,7 @@ export function unregisterErrorHandler() { /** * Wrap callbacks in a wrapper that allows to pass errors to a configured `errorHandler` method. */ -export const callbackWrapper = (cb: T): T => { +export const callbackWrapper = (cb: T, errorSource?: string): T => { if (!errorHandler) { return cb; } @@ -24,6 +24,13 @@ export const callbackWrapper = (cb: T): T => { try { return cb(...rest); } catch (error) { + // Some libraries (styled-components) rely on an exception to be thrown. + // Attach an optional property here to allow `errorHandler` to make + // additional decisions (e.g. to re-throw). + if (errorSource) { + error.__source__ = errorSource; + } + if (errorHandler && errorHandler(error) === true) { return () => { // This will get called by `record()`'s cleanup function diff --git a/packages/rrweb/src/record/observer.ts b/packages/rrweb/src/record/observer.ts index 53e0f7e45f..409c9b7c56 100644 --- a/packages/rrweb/src/record/observer.ts +++ b/packages/rrweb/src/record/observer.ts @@ -724,6 +724,7 @@ function initStyleSheetObserver( } return target.apply(thisArg, argumentsList); }, + 'CSSStyleSheet.insertRule', ), }); @@ -891,6 +892,7 @@ function initStyleSheetObserver( } return target.apply(thisArg, argumentsList); }, + 'CSSStyleSheet.insertRule', ), }, ); From dc3eeafc1a39e7b04c58ea3221198c3f4e84d73e Mon Sep 17 00:00:00 2001 From: billyvg Date: Thu, 12 Oct 2023 18:05:42 +0000 Subject: [PATCH 2/2] Apply formatting changes --- packages/rrweb/src/record/error-handler.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/rrweb/src/record/error-handler.ts b/packages/rrweb/src/record/error-handler.ts index 4fd3fcf370..77efcc907a 100644 --- a/packages/rrweb/src/record/error-handler.ts +++ b/packages/rrweb/src/record/error-handler.ts @@ -15,7 +15,10 @@ export function unregisterErrorHandler() { /** * Wrap callbacks in a wrapper that allows to pass errors to a configured `errorHandler` method. */ -export const callbackWrapper = (cb: T, errorSource?: string): T => { +export const callbackWrapper = ( + cb: T, + errorSource?: string, +): T => { if (!errorHandler) { return cb; }