Skip to content

Commit

Permalink
Merge pull request #8 from amplitude/external-test-2
Browse files Browse the repository at this point in the history
fix(rrweb): external function errors should be tagged
  • Loading branch information
jackson-amplitude authored Jun 6, 2024
2 parents ba5958b + 39adcd9 commit d5a6553
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-gifts-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@amplitude/rrweb': patch
---

fix(rrweb): external function errors should be tagged
19 changes: 19 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,20 @@ 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;
}
};
}
10 changes: 7 additions & 3 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,9 @@ function initStyleSheetObserver(
adds: [{ rule, index }],
});
}
return target.apply(thisArg, argumentsList);
return externalFunctionWrapper(() =>
target.apply(thisArg, argumentsList),
)();
},
),
});
Expand Down Expand Up @@ -694,7 +696,9 @@ function initStyleSheetObserver(
removes: [{ index }],
});
}
return target.apply(thisArg, argumentsList);
return externalFunctionWrapper(() =>
target.apply(thisArg, argumentsList),
)();
},
),
});
Expand Down

0 comments on commit d5a6553

Please sign in to comment.