From 390b65fa1963c4ff55b47fc6d94f6626daedf8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jakubowski?= Date: Sat, 16 Nov 2024 11:38:47 +0100 Subject: [PATCH] Handle errors - display a dialog with a link to create an issue --- src/extension.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 2bef804..4377496 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,16 +6,16 @@ import { } from "./commands"; const DART_MODE = { language: "dart", scheme: "file" }; - function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand( "widgetbook-generator.generate.directory", - generateWidgetbookEntriesForDirectory + (uri: vscode.Uri) => + runCatching(() => generateWidgetbookEntriesForDirectory(uri)) ), vscode.commands.registerCommand( "widgetbook-generator.generate.widget", - generateWidgetbookEntryForWidgetInScope + () => runCatching(() => generateWidgetbookEntryForWidgetInScope()) ), vscode.languages.registerCodeActionsProvider( DART_MODE, @@ -24,4 +24,28 @@ function activate(context: vscode.ExtensionContext) { ); } +async function runCatching(callback: () => Promise) { + const createIssueText = "Create issue"; + + try { + await callback(); + } catch (error) { + vscode.window + .showErrorMessage( + `Failed to execute action. Error: ${error}. ` + + `Please create an issue in extension's repository with minimal reproducible example.`, + createIssueText + ) + .then((selected) => { + if (selected === createIssueText) { + vscode.env.openExternal( + vscode.Uri.parse( + "https://github.com/leancodepl/widgetbook-entries-generator/issues" + ) + ); + } + }); + } +} + export { activate };