Skip to content

Commit 5fea8ad

Browse files
committed
fix circular reference for JSON.stringify
1 parent a760070 commit 5fea8ad

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/external-worker-client.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ExternalWorkerClient {
5252
}
5353
} catch (e) {
5454
console.error('Failed to execute job with exception', e);
55-
await this._restClient.failJob({jobId, errorMessage: typeof e === 'string' ? e : JSON.stringify(e)});
55+
await this._restClient.failJob({jobId, errorMessage: typeof e === 'string' ? e : this.stringify(e)});
5656
}
5757
}
5858
if (timeoutInformation.unsubscribed) {
@@ -61,6 +61,23 @@ export class ExternalWorkerClient {
6161
} while (jobs.length > 0);
6262
timeoutInformation.timeout = setTimeout(() => this.consume(params, timeoutInformation), timeoutInformation.waitMs);
6363
}
64+
65+
private stringify(obj) {
66+
let cache = [];
67+
let str = JSON.stringify(obj, function (key, value) {
68+
if (typeof value === "object" && value !== null) {
69+
if (cache.indexOf(value) !== -1) {
70+
// Circular reference found, discard key
71+
return;
72+
}
73+
// Store value in our collection
74+
cache.push(value);
75+
}
76+
return value;
77+
});
78+
cache = null; // reset the cache
79+
return str;
80+
}
6481
}
6582

6683
class TimeoutInformation {

0 commit comments

Comments
 (0)