From f6b301fbcbe73174546ae26483032e65d7580d18 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Fri, 20 Aug 2021 17:51:56 +0100 Subject: [PATCH] * If there's an error during installation, don't make the version as updated. * throw during createModel if it fails --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/AnkiCardProvider.ts | 2 +- src/extension.ts | 28 ++++++++++++---------------- src/initialSetup.ts | 6 ++++-- src/manageTemplate.ts | 8 +++++++- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc62ad1..b051748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to the "anki" extension will be documented in this file. Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.2.3] + +- Throw an error if the template is not installed correctly, allowing the user to try again. + ## [1.2.1] - Added more diagnostics and logging surrounding the bug of missing note types diff --git a/package.json b/package.json index 209b5c0..12fadc5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "anki", "displayName": "Anki for VSCode", "description": "Sync notes with your locally running Anki", - "version": "1.2.2", + "version": "1.2.3", "publisher": "jasew", "engines": { "vscode": "^1.47.0" diff --git a/src/AnkiCardProvider.ts b/src/AnkiCardProvider.ts index d747cb3..d52d1fc 100644 --- a/src/AnkiCardProvider.ts +++ b/src/AnkiCardProvider.ts @@ -77,7 +77,7 @@ export class AnkiCardProvider implements TreeDataProvider { try { decks = await this.ankiService.deckNamesAndIds(); } catch (e) { - window.showErrorMessage("Failed to get any Anki Decks"); + window.showErrorMessage("Failed to get any Decks. Is Anki running?"); return; } diff --git a/src/extension.ts b/src/extension.ts index 2a2e310..44d23cf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,27 +1,23 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below -import { - window, - extensions, - ExtensionContext, - workspace, - Disposable, -} from "vscode"; -import { AnkiService } from "./AnkiService"; -import { AnkiCardProvider } from "./AnkiCardProvider"; import { getExtensionLogger, IVSCodeExtLogger, - LogLevel, + LogLevel } from "@vscode-logging/logger"; -import { initLogger, getLogger } from "./logger"; -import { registerCommands } from "./commands"; import semver from "semver"; -import { subscriptions } from "./subscriptions"; +import { + ExtensionContext, extensions, window, workspace +} from "vscode"; +import { AnkiCardProvider } from "./AnkiCardProvider"; +import { AnkiService } from "./AnkiService"; +import { registerCommands } from "./commands"; import { AnkiFS, initFilesystem } from "./fileSystemProvider"; -import { initState, getAnkiState } from "./state"; import { initialSetup } from "./initialSetup"; -import { isTemplateInstalled, updateTemplate } from "./manageTemplate"; +import { getLogger, initLogger } from "./logger"; +import { createOrUpdateTemplate, isTemplateInstalled } from "./manageTemplate"; +import { getAnkiState, initState } from "./state"; +import { subscriptions } from "./subscriptions"; require("./resources/vscodeAnkiPlugin.scss"); @@ -95,7 +91,7 @@ export async function activate(context: ExtensionContext) { templateInstalled = await isTemplateInstalled(extContext); getLogger().info(`Status of note type on Anki: ${templateInstalled}`); if (!templateInstalled) { - await updateTemplate(extContext); + await createOrUpdateTemplate(extContext); } } else { getLogger().info('Could not connect to Anki'); diff --git a/src/initialSetup.ts b/src/initialSetup.ts index 1321163..9e95caf 100644 --- a/src/initialSetup.ts +++ b/src/initialSetup.ts @@ -40,11 +40,13 @@ export async function initialSetup(ctx: IContext) { await createOrUpdateTemplate(ctx); result = await ctx.ankiService.storeMultipleFiles(resources); disposable.dispose(); - } catch (e) { + } catch (e: any) { window.showErrorMessage( - "Anki Installation: Unable to update resources on Anki" + "Anki Installation: Unable to update resources on Anki, please make sure Anki is running and try again" ); ctx.logger.error(e); + // If any of the above failed we don't want to update the version, meaning it will try again next time the extension has started + return; } // If assets are safely installed we can set a flag so we don't need to do this action again diff --git a/src/manageTemplate.ts b/src/manageTemplate.ts index 3e7c77e..7caf8eb 100644 --- a/src/manageTemplate.ts +++ b/src/manageTemplate.ts @@ -62,8 +62,14 @@ export async function createOrUpdateTemplate(ctx: IContext) { getLogger().info( `${CONSTANTS.defaultTemplateName} was not found in Anki. Will attempt to upload..` ); + let result; + try { + result = await ctx.ankiService.createModel(model); + } catch(e) { + getLogger().error(`Creating the template on Anki has failed: ${e}`); + throw new Error(`Failed to upload template! ${e}`); + } - const result = await ctx.ankiService.createModel(model); if (result.error) { getLogger().error(`Failed to upload template: ${result.error}`); throw new Error("Failed to upload template!");