Skip to content

Commit 1cb5ec6

Browse files
authored
Merge pull request #125 from codex-team/fix-strignify
fix(console-catcher): handle stringify error
2 parents f0298ce + 9d9ccba commit 1cb5ec6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hawk.so/javascript",
33
"type": "commonjs",
4-
"version": "3.2.5",
4+
"version": "3.2.6",
55
"description": "JavaScript errors tracking for Hawk.so",
66
"files": [
77
"dist"

src/addons/consoleCatcher.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ function createConsoleCatcher(): {
2020
* Converts any argument to its string representation
2121
*
2222
* @param arg - Value to convert to string
23+
* @throws Error if the argument can not be stringified, for example by such reason:
24+
* SecurityError: Failed to read a named property 'toJSON' from 'Window': Blocked a frame with origin "https://codex.so" from accessing a cross-origin frame.
2325
*/
2426
function stringifyArg(arg: unknown): string {
2527
if (typeof arg === 'string') {
@@ -52,7 +54,13 @@ function createConsoleCatcher(): {
5254

5355
if (typeof firstArg !== 'string' || !firstArg.includes('%c')) {
5456
return {
55-
message: args.map(stringifyArg).join(' '),
57+
message: args.map(arg => {
58+
try {
59+
return stringifyArg(arg);
60+
} catch (error) {
61+
return '[Error stringifying argument: ' + (error instanceof Error ? error.message : String(error)) + ']';
62+
}
63+
}).join(' '),
5664
styles: [],
5765
};
5866
}
@@ -76,7 +84,13 @@ function createConsoleCatcher(): {
7684
// Add remaining arguments that aren't styles
7785
const remainingArgs = args
7886
.slice(styles.length + 1)
79-
.map(stringifyArg)
87+
.map(arg => {
88+
try {
89+
return stringifyArg(arg);
90+
} catch (error) {
91+
return '[Error stringifying argument: ' + (error instanceof Error ? error.message : String(error)) + ']';
92+
}
93+
})
8094
.join(' ');
8195

8296
return {

0 commit comments

Comments
 (0)