Skip to content

Commit

Permalink
Fix "Converting circular structure to JSON" Error
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed May 11, 2019
1 parent dd9eb6b commit 50036a2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/plugin-ext/src/api/rpc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ class MessageFactory {
if (messageToSendHostId) {
prefix = `"hostID":"${messageToSendHostId}",`;
}
return `{${prefix}"type":${MessageType.Request},"id":"${req}","proxyId":"${rpcId}","method":"${method}","args":${JSON.stringify(args, ObjectsTransferrer.replacer)}}`;
return `{${prefix}"type":${MessageType.Request},"id":"${req}","proxyId":"${rpcId}","method":"${method}","args":
${JSON.stringify(args, (key, value) => ObjectsTransferrer.replacer([], key, value))}}`;
}

public static replyOK(req: string, res: any, messageToSendHostId?: string): string {
Expand All @@ -329,7 +330,7 @@ class MessageFactory {
if (typeof res === 'undefined') {
return `{${prefix}"type":${MessageType.Reply},"id":"${req}"}`;
}
return `{${prefix}"type":${MessageType.Reply},"id":"${req}","res":${JSON.stringify(res, ObjectsTransferrer.replacer)}}`;
return `{${prefix}"type":${MessageType.Reply},"id":"${req}","res":${JSON.stringify(res, (key, value) => ObjectsTransferrer.replacer([], key, value))}}`;
}

public static replyErr(req: string, err: any, messageToSendHostId?: string): string {
Expand Down Expand Up @@ -357,7 +358,7 @@ class MessageFactory {
namespace ObjectsTransferrer {

// tslint:disable-next-line:no-any
export function replacer(key: string | undefined, value: any): any {
export function replacer(cache: any[], key: string | undefined, value: any): any {
if (value instanceof URI) {
return {
$type: SerializedObjectType.THEIA_URI,
Expand Down Expand Up @@ -387,6 +388,15 @@ namespace ObjectsTransferrer {
$type: SerializedObjectType.VSCODE_URI,
data: uri.toString()
} as SerializedObject;
} else if (value && typeof value === 'object') {
if (cache.indexOf(value) !== -1) {
try {
return JSON.parse(JSON.stringify(value));
} catch (e) {
return;
}
}
cache.push(value);
}

return value;
Expand Down

0 comments on commit 50036a2

Please sign in to comment.