From 5b43717c5c94b2821f7eab9e69be51c854daefd3 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Tue, 4 Aug 2020 13:41:19 +0200 Subject: [PATCH] feat: when a widget fails, show a fallback error widget --- js/src/manager.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/js/src/manager.js b/js/src/manager.js index 3f927d4dc..5845aa726 100644 --- a/js/src/manager.js +++ b/js/src/manager.js @@ -31,6 +31,7 @@ import { MessageLoop } from '@phosphor/messaging'; import { requireLoader } from './loader'; import { batchRateMap } from './utils'; +import * as errorwidget from './errorwidget'; if (typeof window !== "undefined" && typeof window.define !== "undefined") { window.define("@jupyter-widgets/base", base); @@ -49,6 +50,8 @@ if (typeof window !== "undefined" && typeof window.define !== "undefined") { window.define("@phosphor/algorithm", PhosphorAlgorithm); window.define("@phosphor/commands", PhosphorCommands); window.define("@phosphor/domutils", PhosphorDomutils); + + window.define("voila-errorwidget", errorwidget); } const WIDGET_MIMETYPE = 'application/vnd.jupyter.widget-view+json'; @@ -106,10 +109,19 @@ export class WidgetManager extends JupyterLabManager { } async loadClass(className, moduleName, moduleVersion) { + try { + return await this._loadClass(className, moduleName, moduleVersion) + } catch(error) { + return errorwidget.createErrorWidget(error); + }; + } + + async _loadClass(className, moduleName, moduleVersion) { if ( moduleName === '@jupyter-widgets/base' || moduleName === '@jupyter-widgets/controls' || - moduleName === '@jupyter-widgets/output' + moduleName === '@jupyter-widgets/output' || + moduleName === 'voila-errorwidget' ) { return super.loadClass(className, moduleName, moduleVersion); } @@ -120,7 +132,7 @@ export class WidgetManager extends JupyterLabManager { return module[className]; } else { - return Promise.reject("Class " + className + " not found in module " + moduleName + "@" + moduleVersion); + throw Error("Class " + className + " not found in module " + moduleName + "@" + moduleVersion); } }) } @@ -145,6 +157,11 @@ export class WidgetManager extends JupyterLabManager { version: output.OUTPUT_WIDGET_VERSION, exports: output }); + this.register({ + name: 'voila-errorwidget', + version: '1.0.0', + exports: errorwidget + }); } async _build_models() {