From a2652247011fbd97835adb82969de94a4ced3e00 Mon Sep 17 00:00:00 2001 From: Cory Rivera Date: Thu, 9 Jun 2022 12:32:42 -0700 Subject: [PATCH] Show an error in cell output when trying to load an unsupported output type. (#19693) --- .../notebook/browser/models/outputProcessor.ts | 3 +++ .../notebook/test/browser/outputProcessor.test.ts | 14 ++++++++++++++ .../notebook/common/localContentManager.ts | 8 +++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/models/outputProcessor.ts b/src/sql/workbench/contrib/notebook/browser/models/outputProcessor.ts index 967d07eee7..b505393d9a 100644 --- a/src/sql/workbench/contrib/notebook/browser/models/outputProcessor.ts +++ b/src/sql/workbench/contrib/notebook/browser/models/outputProcessor.ts @@ -8,6 +8,7 @@ import { nb } from 'azdata'; import { JSONObject, isPrimitive } from 'sql/workbench/services/notebook/common/jsonext'; import { nbformat } from 'sql/workbench/services/notebook/common/nbformat'; import { MimeModel } from 'sql/workbench/services/notebook/browser/outputs/mimemodel'; +import * as nls from 'vs/nls'; /** * A multiline string. @@ -46,6 +47,8 @@ export function getData(output: nb.ICellOutput): JSONObject { } else if (output.evalue) { bundle['application/vnd.jupyter.stderr'] = output.ename ? `${output.ename}: ${output.evalue}` : `${output.evalue}`; } + } else { + bundle['application/vnd.jupyter.stderr'] = nls.localize('notebookInvalidOutputTypeError', "Output type '{0}' not recognized.", output.output_type); } return convertBundle(bundle); } diff --git a/src/sql/workbench/contrib/notebook/test/browser/outputProcessor.test.ts b/src/sql/workbench/contrib/notebook/test/browser/outputProcessor.test.ts index 246ed3a9fe..7083e3724a 100644 --- a/src/sql/workbench/contrib/notebook/test/browser/outputProcessor.test.ts +++ b/src/sql/workbench/contrib/notebook/test/browser/outputProcessor.test.ts @@ -67,6 +67,20 @@ suite('OutputProcessor functions', function (): void { verifyGetDataForStreamOutput(output); }); } + + // unknown output types + test('Should report an error for unknown output types', () => { + const output = { + output_type: 'unknown', + data: { + 'text/html': 'Test text' + }, + metadata: {} + }; + const result = op.getData(output); + assert(result['application/vnd.jupyter.stderr'] !== undefined, 'Should set an error message after receiving unknown output type.'); + assert(result['text/html'] === undefined, 'Should not add any data after receiving unknown output type.'); + }); }); suite('getMetadata', function (): void { diff --git a/src/sql/workbench/services/notebook/common/localContentManager.ts b/src/sql/workbench/services/notebook/common/localContentManager.ts index e50497acbc..de32b9b4da 100644 --- a/src/sql/workbench/services/notebook/common/localContentManager.ts +++ b/src/sql/workbench/services/notebook/common/localContentManager.ts @@ -155,8 +155,8 @@ namespace v4 { traceback: output.traceback }; default: - // Should never get here - throw new TypeError(localize('unrecognizedOutput', "Output type {0} not recognized", (output).output_type)); + // Unknown type, so return as is. If it's unsupported, then an error will be shown later when trying to render it. + return output; } } @@ -272,6 +272,7 @@ namespace v3 { name: name, text: v4.demultiline(output.text) }; + case 'error': case 'pyerr': return { output_type: OutputTypes.Error, @@ -280,7 +281,8 @@ namespace v3 { traceback: output.traceback }; default: - throw new TypeError(localize('unrecognizedOutputType', "Output type {0} not recognized", output.output_type)); + // Unknown type, so return as is. If it's unsupported, then an error will be shown later when trying to render it. + return output; } };