Skip to content

Commit e6b0e7d

Browse files
committed
handle input file parsing errors - fixes #1
1 parent ba6df51 commit e6b0e7d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Controller.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@ export class Controller {
6666
}
6767

6868
const document = await vscode.workspace.openTextDocument(selectedFileUri);
69-
inputContent = JSON.parse(document.getText());
69+
try {
70+
inputContent = JSON.parse(document.getText());
71+
}
72+
catch (error: any) {
73+
execution.replaceOutput([
74+
new vscode.NotebookCellOutput([
75+
vscode.NotebookCellOutputItem.text(`Error parsing JSON file. Please check the file and try again. \n\n${error.message}`),
76+
])
77+
]);
78+
execution.end(false, Date.now());
79+
return;
80+
}
7081

7182
if (isMainThread) {
7283
const worker = new Worker(join(__filename, '../worker.js'), {

src/Serializer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class Serializer implements vscode.NotebookSerializer {
5050
let contents: RawNotebookCell[] = [];
5151

5252
for (const cell of data.cells) {
53-
const rawOutput = cell?.outputs?.at(0)?.items[0].data;
53+
const outputItem = cell?.outputs?.at(0)?.items[0];
54+
const rawOutput = outputItem?.mime === 'text/x-json' ? outputItem.data : undefined;
5455
let output = undefined;
5556
if (rawOutput) {
5657
output = JSON.parse(new TextDecoder().decode(rawOutput));

0 commit comments

Comments
 (0)