Skip to content

Commit d7a92e0

Browse files
committed
feat: safe stringify
1 parent 5665774 commit d7a92e0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

libraries/nestjs-libraries/src/integrations/social.abstract.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ export class NotEnoughScopes {
3030
constructor(public message = 'Not enough scopes') {}
3131
}
3232

33+
function safeStringify(obj: any) {
34+
const seen = new WeakSet();
35+
36+
return JSON.stringify(obj, (key, value) => {
37+
if (typeof value === 'object' && value !== null) {
38+
if (seen.has(value)) {
39+
return '[Circular]';
40+
}
41+
seen.add(value);
42+
}
43+
return value;
44+
});
45+
}
46+
3347
export abstract class SocialAbstract {
3448
abstract identifier: string;
3549
maxConcurrentJob = 1;
@@ -62,20 +76,20 @@ export abstract class SocialAbstract {
6276
try {
6377
value = await func();
6478
} catch (err) {
65-
const handle = this.handleErrors(JSON.stringify(err));
79+
const handle = this.handleErrors(safeStringify(err));
6680
value = { err: true, value: 'Unknown Error', ...(handle || {}) };
6781
}
6882

6983
if (value && value?.err && value?.value) {
7084
if (value.type === 'refresh-token') {
7185
throw new RefreshToken(
7286
'',
73-
JSON.stringify({}),
87+
safeStringify({}),
7488
{} as any,
7589
value.value || ''
7690
);
7791
}
78-
throw new BadBody('', JSON.stringify({}), {} as any, value.value || '');
92+
throw new BadBody('', safeStringify({}), {} as any, value.value || '');
7993
}
8094

8195
return value;

0 commit comments

Comments
 (0)