Fix #3937 Create new notebook (Mac) and receive TypeError (#3965)

- Handles empty file scenario, with fixes along the way for missing metadata (bonus win)
- In non-empty file still shows error and kernel stuck in loading state. #3964 opened to track this issue and fix later
This commit is contained in:
Kevin Cunnane
2019-02-07 10:35:19 -08:00
committed by GitHub
parent 40e0d5cfbf
commit 69dff5a2cb
3 changed files with 49 additions and 23 deletions

View File

@@ -17,6 +17,7 @@ import { localize } from 'vs/nls';
import { JSONObject } from 'sql/parts/notebook/models/jsonext';
import { OutputTypes } from 'sql/parts/notebook/models/contracts';
import { nbversion } from 'sql/parts/notebook/notebookConstants';
import { nbformat } from 'sql/parts/notebook/models/nbformat';
type MimeBundle = { [key: string]: string | string[] | undefined };
@@ -29,7 +30,8 @@ export class LocalContentManager implements nb.ContentManager {
let path = notebookUri.fsPath;
// Note: intentionally letting caller handle exceptions
let notebookFileBuffer = await pfs.readFile(path);
let contents: JSONObject = json.parse(notebookFileBuffer.toString());
let stringContents = notebookFileBuffer.toString();
let contents: JSONObject = json.parse(stringContents);
if (contents) {
if (contents.nbformat === 4) {
@@ -40,9 +42,13 @@ export class LocalContentManager implements nb.ContentManager {
if (contents.nbformat) {
throw new TypeError(localize('nbformatNotRecognized', 'nbformat v{0}.{1} not recognized', contents.nbformat, contents.nbformat_minor));
}
} else if (stringContents === '' || stringContents === undefined) {
// Empty?
return v4.createEmptyNotebook();
}
// else, fallthrough condition
throw new TypeError(localize('nbNotSupported', 'This notebook format is not supported'));
throw new TypeError(localize('nbNotSupported', 'This file does not have a valid notebook format'));
}
@@ -72,6 +78,15 @@ namespace v4 {
return notebook;
}
export function createEmptyNotebook(): nb.INotebookContents {
return {
cells: [],
metadata: undefined,
nbformat: nbformat.MAJOR_VERSION,
nbformat_minor: nbformat.MINOR_VERSION
};
}
function readCell(cell: nb.ICellContents): nb.ICellContents {
switch (cell.cell_type) {
case 'markdown':