diff --git a/src/index.ts b/src/index.ts index 26ce528..0128567 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { ILayoutRestorer } from "@jupyterlab/application"; -import { IThemeManager, WidgetTracker } from "@jupyterlab/apputils"; +import { IThemeManager, showErrorMessage, WidgetTracker } from "@jupyterlab/apputils"; import { IDefaultDrive } from "@jupyterlab/services"; import { ITranslator } from "@jupyterlab/translation"; import type { JupyterFrontEnd, JupyterFrontEndPlugin } from "@jupyterlab/application"; @@ -154,24 +154,30 @@ function activateArrowGrid( app.docRegistry.addWidgetFactory(factory); factory.widgetCreated.connect(async (_sender, widget) => { - // Track the widget. - void tracker.add(widget); - // Notify the widget tracker if restore data needs to update. - widget.context.pathChanged.connect(() => { - void tracker.save(widget); - }); + try { + // Track the widget. + void tracker.add(widget); + // Notify the widget tracker if restore data needs to update. + widget.context.pathChanged.connect(() => { + void tracker.save(widget); + }); - if (csv_ft) { - widget.title.icon = csv_ft.icon; - widget.title.iconClass = csv_ft.iconClass!; - widget.title.iconLabel = csv_ft.iconLabel!; + if (csv_ft) { + widget.title.icon = csv_ft.icon; + widget.title.iconClass = csv_ft.iconClass!; + widget.title.iconLabel = csv_ft.iconLabel!; + } + await widget.content.ready; + widget.content.style = style; + widget.content.rendererConfig = rendererConfig; + updateThemes(); + console.log("JupyterLab extension arbalister is activated!"); + } catch (error) { + await showErrorMessage( + trans.__("ArrowGridViewer widget initialization failed"), + error as Error, + ); } - await widget.content.ready; - widget.content.style = style; - widget.content.rendererConfig = rendererConfig; - updateThemes(); - - console.log("JupyterLab extension arbalister is activated!"); }); const updateThemes = (newTheme?: string | null) => { @@ -192,8 +198,15 @@ function activateArrowGrid( }; if (themeManager) { themeManager.themeChanged.connect((_, args) => { - const newTheme = args.newValue; - updateThemes(newTheme); + try { + const newTheme = args.newValue; + updateThemes(newTheme); + } catch (error) { + void showErrorMessage( + trans.__("Failed to the viewer according to updated theme"), + error as Error, + ); + } }); } } diff --git a/src/widget.ts b/src/widget.ts index 6ed9669..9f76e33 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -1,3 +1,4 @@ +import { Dialog, showDialog } from "@jupyterlab/apputils"; import { ABCWidgetFactory, DocumentWidget } from "@jupyterlab/docregistry"; import { PromiseDelegate } from "@lumino/coreutils"; import { BasicKeyHandler, BasicMouseHandler, DataGrid, TextRenderer } from "@lumino/datagrid"; @@ -73,9 +74,27 @@ export class ArrowGridViewer extends Panel { } private async _updateGrid() { - const model = new ArrowModel({ path: this.path }); - await model.ready; - this._grid.dataModel = model; + try { + const model = new ArrowModel({ path: this.path }); + await model.ready; + this._grid.dataModel = model; + } catch (error) { + const trans = Dialog.translator.load("jupyterlab"); + const buttons = [ + Dialog.cancelButton({ label: trans.__("Close") }), + Dialog.okButton({ label: trans.__("Retry") }), + ]; + const confirm = await showDialog({ + title: "Failed to initialized ArrowGridViewer", + body: typeof error === "string" ? error : (error as Error).message, + buttons, + }); + const shouldRetry = confirm.button.accept; + + if (shouldRetry) { + await this._updateGrid(); + } + } } private async _updateRenderer(): Promise {