diff --git a/extension/src/DataSource/DataExtractionProvider/GenericDataExtractionProviderFactory.ts b/extension/src/DataSource/DataExtractionProvider/GenericDataExtractionProviderFactory.ts index b59ebaf..ad34eec 100644 --- a/extension/src/DataSource/DataExtractionProvider/GenericDataExtractionProviderFactory.ts +++ b/extension/src/DataSource/DataExtractionProvider/GenericDataExtractionProviderFactory.ts @@ -36,12 +36,49 @@ class GenericDataExtractionProvider implements DataExtractionProvider { expression, frameId, }); - const resultStr = reply.result; - const jsonData = resultStr.substr(1, resultStr.length - 2); - const result = JSON.parse(jsonData) as ExtractedData; + + function isEnclosedWith(str: string, char: string): boolean { + return str.startsWith(char) && str.endsWith(char); + } + + const jsonData = reply.result.trim(); + + function parseJson(str: string) { + try { + return JSON.parse(str); + } catch (error) { + throw new Error( + `Could not parse \`${str}\` as JSON.\n${error.message}` + ); + } + } + + let result; + + try { + let jsonData2; + // Remove optionally enclosing characters. + if ( + isEnclosedWith(jsonData, '"') || + isEnclosedWith(jsonData, "'") + ) { + // In case of JavaScript: `"{ "kind": { ... }, "text": "some\ntext" }"` + jsonData2 = jsonData.substr(1, jsonData.length - 2); + } else { + // Just in case no quoting is done. + jsonData2 = jsonData; + } + result = parseJson(jsonData2); + } catch (e) { + // in case of C++: `"{ \"kind\": { ... }, \"text\": \"some\\ntext\" }"` + const str = parseJson(jsonData); + // str is now `{ "kind": { ... }, "text": "some\ntext" }"` + result = parseJson(str); + // result is now { kind: { ... }, text: "some\ntext" } + } if (!isExtractedData(result)) { - throw new Error("Invalid Data"); + throw new Error("Data does not match ExtractedData interface."); } return { diff --git a/extension/src/VsCodeDebugger.ts b/extension/src/VsCodeDebugger.ts index c35a046..31fc397 100644 --- a/extension/src/VsCodeDebugger.ts +++ b/extension/src/VsCodeDebugger.ts @@ -149,6 +149,9 @@ export class VsCodeDebugSession { frameId: args.frameId, column: args.column, }); + if (!reply) { + return []; + } return reply.targets; } catch (error) { console.error(error);