diff --git a/src/sql/workbench/services/notebook/common/localContentManager.ts b/src/sql/workbench/services/notebook/common/localContentManager.ts index 6f22d65e6e..64f76f37eb 100644 --- a/src/sql/workbench/services/notebook/common/localContentManager.ts +++ b/src/sql/workbench/services/notebook/common/localContentManager.ts @@ -25,8 +25,12 @@ export class LocalContentManager implements nb.ContentManager { constructor(@IFileService private readonly fileService: IFileService) { } public async loadFromContentString(contentString: string): Promise { - let contents: JSONObject = json.parse(contentString); - + let contents: JSONObject; + if (contentString === '' || contentString === undefined) { + return v4.createEmptyNotebook(); + } else { + contents = this.parseFromJson(contentString); + } if (contents) { if (contents.nbformat === 4) { return v4.readNotebook(contents); @@ -36,9 +40,6 @@ export class LocalContentManager implements nb.ContentManager { if (contents.nbformat) { throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any)); } - } else if (contentString === '' || contentString === undefined) { - // Empty? - return v4.createEmptyNotebook(); } // else, fallthrough condition @@ -53,7 +54,13 @@ export class LocalContentManager implements nb.ContentManager { // Note: intentionally letting caller handle exceptions let notebookFileBuffer = await this.fileService.readFile(notebookUri); let stringContents = notebookFileBuffer.value.toString(); - let contents: JSONObject = json.parse(stringContents); + let contents: JSONObject; + if (stringContents === '' || stringContents === undefined) { + // Empty? + return v4.createEmptyNotebook(); + } else { + contents = this.parseFromJson(stringContents); + } if (contents) { if (contents.nbformat === 4) { @@ -64,9 +71,6 @@ export class LocalContentManager implements nb.ContentManager { if (contents.nbformat) { throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat as any, contents.nbformat_minor as any)); } - } else if (stringContents === '' || stringContents === undefined) { - // Empty? - return v4.createEmptyNotebook(); } // else, fallthrough condition @@ -81,6 +85,15 @@ export class LocalContentManager implements nb.ContentManager { return notebook; } + private parseFromJson(contentString: string): JSONObject { + let contents: JSONObject; + try { + contents = JSON.parse(contentString); + } catch { + contents = json.parse(contentString); + } + return contents; + } } namespace v4 {