From dad532646286deb0f90ec47d20bfb30b8647d0e7 Mon Sep 17 00:00:00 2001 From: Ryan Vandersmith Date: Tue, 22 Aug 2023 11:06:11 -0600 Subject: [PATCH] Add notification on Mops / Vessel package configuration error (#240) * Add notification for error in Mops / Vessel configuration * Add 'View logs' button to error message * Adjust error message detail rendering --- src/common/connectionTypes.ts | 9 +++++++++ src/extension.ts | 10 ++++++++++ src/server/server.ts | 23 +++++++++++++++-------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/common/connectionTypes.ts b/src/common/connectionTypes.ts index 1bd9c39..a94b29c 100644 --- a/src/common/connectionTypes.ts +++ b/src/common/connectionTypes.ts @@ -36,3 +36,12 @@ export const DEPLOY_PLAYGROUND_MESSAGE = export interface NotifyDeployParams { message: string; } + +export const ERROR_MESSAGE = new NotificationType( + 'vscode-motoko/notify-error', +); + +export interface NotifyErrorParams { + message: string; + detail?: string | undefined; +} diff --git a/src/extension.ts b/src/extension.ts index efe48c6..2de05e8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -31,6 +31,7 @@ import * as which from 'which'; import { DEPLOY_PLAYGROUND, DEPLOY_PLAYGROUND_MESSAGE, + ERROR_MESSAGE, TEST_FILE_REQUEST, TestParams, TestResult, @@ -326,6 +327,15 @@ function restartLanguageServer( serverOptions, clientOptions, ); + client.onNotification(ERROR_MESSAGE, async ({ message, detail }) => { + const item = await window.showErrorMessage( + detail ? `${message}\n${detail}` : message, + 'View logs', + ); + if (item === 'View logs') { + client.outputChannel.show(); + } + }); client.start().catch((err) => console.error(err.stack || err)); context.subscriptions.push(client); } diff --git a/src/server/server.ts b/src/server/server.ts index 56172d9..9a2a98d 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -37,6 +37,7 @@ import { URI } from 'vscode-uri'; import { DEPLOY_PLAYGROUND, DEPLOY_PLAYGROUND_MESSAGE, + ERROR_MESSAGE, TEST_FILE_REQUEST, TestResult, } from '../common/connectionTypes'; @@ -209,7 +210,6 @@ async function getPackageSources( } let loadingPackages = false; -let packageConfigError = false; let packageConfigChangeTimeout: ReturnType; function notifyPackageConfigChange(reuseCached = false) { if (!reuseCached) { @@ -218,7 +218,6 @@ function notifyPackageConfigChange(reuseCached = false) { loadingPackages = true; clearTimeout(packageConfigChangeTimeout); packageConfigChangeTimeout = setTimeout(async () => { - packageConfigError = false; try { resetContexts(); @@ -278,16 +277,23 @@ function notifyPackageConfigChange(reuseCached = false) { context.motoko.usePackage(name, path); }); } catch (err) { - packageConfigError = true; + connection.sendNotification(ERROR_MESSAGE, { + message: `Error while resolving Motoko packages:`, + detail: String(err).replace(/^Error: /, ''), + }); context.error = String(err); console.warn(err); return; } - } catch (err) { - packageConfigError = true; + } catch (err: any) { + connection.sendNotification(ERROR_MESSAGE, { + message: `Error while loading Motoko packages:`, + detail: String(err).replace(/^Error: /, ''), + }); console.error( `Error while reading packages for directory (${dir}): ${err}`, ); + return; } }), ); @@ -303,10 +309,11 @@ function notifyPackageConfigChange(reuseCached = false) { loadingPackages = false; notifyWorkspace(); // Update virtual file system notifyDfxChange(); // Reload dfx.json - } catch (err) { + } catch (err: any) { loadingPackages = false; - packageConfigError = true; - console.error(`Error while loading packages: ${err}`); + console.error( + `Error while loading packages: ${err?.message || err}`, + ); } }, 1000); }