Skip to content

Commit

Permalink
Handle errors - display a dialog with a link to create an issue
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Nov 16, 2024
1 parent 8984168 commit 390b65f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,4 +24,28 @@ function activate(context: vscode.ExtensionContext) {
);
}

async function runCatching(callback: () => Promise<void>) {
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 };

0 comments on commit 390b65f

Please sign in to comment.