diff --git a/vscode/microsoft-kiota/src/generateClient.ts b/vscode/microsoft-kiota/src/generateClient.ts index 1c51748d5e..ea0be0813b 100644 --- a/vscode/microsoft-kiota/src/generateClient.ts +++ b/vscode/microsoft-kiota/src/generateClient.ts @@ -24,6 +24,7 @@ export function generateClient(context: vscode.ExtensionContext, operation: ConsumerOperation, workingDirectory: string = getWorkspaceJsonDirectory() ): Promise { + try { return connectToKiota(context, async (connection) => { const request = new rpc.RequestType1( "Generate" @@ -51,4 +52,7 @@ export function generateClient(context: vscode.ExtensionContext, } as GenerationConfiguration, ); }, workingDirectory); + } catch (error) { + return Promise.resolve(undefined); + } }; diff --git a/vscode/microsoft-kiota/src/generatePlugin.ts b/vscode/microsoft-kiota/src/generatePlugin.ts index 221b821538..134515ae55 100644 --- a/vscode/microsoft-kiota/src/generatePlugin.ts +++ b/vscode/microsoft-kiota/src/generatePlugin.ts @@ -4,7 +4,7 @@ import { KiotaPluginType } from "./enums"; import { connectToKiota, ConsumerOperation, GenerationConfiguration, KiotaLogEntry } from "./kiotaInterop"; import { getWorkspaceJsonDirectory } from "./util"; -export function generatePlugin(context: vscode.ExtensionContext, +export function generatePlugin(context: vscode.ExtensionContext, descriptionPath: string, output: string, pluginTypes: KiotaPluginType[], @@ -15,7 +15,8 @@ export function generatePlugin(context: vscode.ExtensionContext, cleanOutput: boolean, disableValidationRules: string[], operation: ConsumerOperation, - workingDirectory: string = getWorkspaceJsonDirectory() ): Promise { + workingDirectory: string = getWorkspaceJsonDirectory()): Promise { + try { return connectToKiota(context, async (connection) => { const request = new rpc.RequestType1( "GeneratePlugin" @@ -36,4 +37,7 @@ export function generatePlugin(context: vscode.ExtensionContext, } as GenerationConfiguration, ); }, workingDirectory); + } catch (error) { + return Promise.resolve(undefined); + } }; diff --git a/vscode/microsoft-kiota/src/getKiotaVersion.ts b/vscode/microsoft-kiota/src/getKiotaVersion.ts index a7756b37ab..7e6f3c3852 100644 --- a/vscode/microsoft-kiota/src/getKiotaVersion.ts +++ b/vscode/microsoft-kiota/src/getKiotaVersion.ts @@ -3,6 +3,7 @@ import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; export function getKiotaVersion(context: vscode.ExtensionContext, kiotaOutputChannel: vscode.LogOutputChannel): Promise { + try { return connectToKiota(context, async (connection) => { const request = new rpc.RequestType0("GetVersion"); const result = await connection.sendRequest(request); @@ -17,4 +18,7 @@ export function getKiotaVersion(context: vscode.ExtensionContext, kiotaOutputCha kiotaOutputChannel.show(); return undefined; }); + } catch (error) { + return Promise.resolve(undefined); + } }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/getLanguageInformation.ts b/vscode/microsoft-kiota/src/getLanguageInformation.ts index 917ba3793a..9717f16f5e 100644 --- a/vscode/microsoft-kiota/src/getLanguageInformation.ts +++ b/vscode/microsoft-kiota/src/getLanguageInformation.ts @@ -4,37 +4,44 @@ import * as vscode from "vscode"; let _languageInformation: LanguagesInformation | undefined; // doesn't change over the lifecycle of the extension export async function getLanguageInformation(context: vscode.ExtensionContext): Promise { - if(_languageInformation) - { - return _languageInformation; - } - const result = await getLanguageInformationInternal(context); - if (result) { - _languageInformation = result; - } - return result; + if (_languageInformation) { + return _languageInformation; + } + const result = await getLanguageInformationInternal(context); + if (result) { + _languageInformation = result; + } + return result; }; function getLanguageInformationInternal(context: vscode.ExtensionContext): Promise { - return connectToKiota(context, async (connection) => { - const request = new rpc.RequestType0( + try { + return connectToKiota(context, async (connection) => { + const request = new rpc.RequestType0( "Info" - ); - return await connection.sendRequest( + ); + return await connection.sendRequest( request, - ); - }); + ); + }); + } catch (error) { + return Promise.resolve(undefined); + } }; export function getLanguageInformationForDescription(context: vscode.ExtensionContext, descriptionUrl: string, clearCache: boolean): Promise { - return connectToKiota(context, async (connection) => { - const request = new rpc.RequestType2( + try { + return connectToKiota(context, async (connection) => { + const request = new rpc.RequestType2( "InfoForDescription" - ); - return await connection.sendRequest( + ); + return await connection.sendRequest( request, descriptionUrl, clearCache - ); - }); + ); + }); + } catch (error) { + return Promise.resolve(undefined); + } }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index cc22cc9cc6..2bb530d143 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -5,35 +5,36 @@ import { KiotaGenerationLanguage, KiotaPluginType } from './enums'; import { ensureKiotaIsPresent, getKiotaPath } from './kiotaInstall'; import { getWorkspaceJsonDirectory } from "./util"; -export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise, workingDirectory:string = getWorkspaceJsonDirectory()): Promise { - const kiotaPath = getKiotaPath(context); - await ensureKiotaIsPresent(context); - const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: workingDirectory, - env: { - ...process.env, - // eslint-disable-next-line @typescript-eslint/naming-convention - KIOTA_CONFIG_PREVIEW: "true", +export async function connectToKiota(context: vscode.ExtensionContext, callback: (connection: rpc.MessageConnection) => Promise, workingDirectory: string = getWorkspaceJsonDirectory()): Promise { + const kiotaPath = getKiotaPath(context); + await ensureKiotaIsPresent(context); + const childProcess = cp.spawn(kiotaPath, ["rpc"], { + cwd: workingDirectory, + env: { + ...process.env, + // eslint-disable-next-line @typescript-eslint/naming-convention + KIOTA_CONFIG_PREVIEW: "true", + } + }); + let connection = rpc.createMessageConnection( + new rpc.StreamMessageReader(childProcess.stdout), + new rpc.StreamMessageWriter(childProcess.stdin)); + connection.listen(); + try { + return await callback(connection); + } catch (error) { + const errorMessage = (error as { data?: { message: string } })?.data?.message + || 'An unknown error occurred'; + throw new Error(errorMessage); + } finally { + connection.dispose(); + childProcess.kill(); } - }); - let connection = rpc.createMessageConnection( - new rpc.StreamMessageReader(childProcess.stdout), - new rpc.StreamMessageWriter(childProcess.stdin)); - connection.listen(); - try { - return await callback(connection); - } catch (error) { - console.error(error); - return undefined; - } finally { - connection.dispose(); - childProcess.kill(); - } } export interface KiotaLogEntry { - level: LogLevel; - message: string; + level: LogLevel; + message: string; } export interface KiotaOpenApiNode { @@ -76,16 +77,16 @@ export interface KiotaSearchResult extends KiotaLoggedResult { results: Record; } export interface KiotaSearchResultItem { - // eslint-disable-next-line @typescript-eslint/naming-convention - Title: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - Description: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - ServiceUrl?: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - DescriptionUrl?: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - VersionLabels?: string[]; + // eslint-disable-next-line @typescript-eslint/naming-convention + Title: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + Description: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + ServiceUrl?: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + DescriptionUrl?: string; + // eslint-disable-next-line @typescript-eslint/naming-convention + VersionLabels?: string[]; } export enum ConsumerOperation { diff --git a/vscode/microsoft-kiota/src/migrateFromLockFile.ts b/vscode/microsoft-kiota/src/migrateFromLockFile.ts index b1efb59f8a..c1c1d3acdf 100644 --- a/vscode/microsoft-kiota/src/migrateFromLockFile.ts +++ b/vscode/microsoft-kiota/src/migrateFromLockFile.ts @@ -5,42 +5,46 @@ import { KIOTA_LOCK_FILE } from "./constants"; import { getWorkspaceJsonPath, handleMigration } from "./util"; export function migrateFromLockFile(context: vscode.ExtensionContext, lockFileDirectory: string): Promise { - return connectToKiota(context, async (connection) => { - const request = new rpc.RequestType1( - "MigrateFromLockFile" - ); - const result = await connection.sendRequest( - request, - lockFileDirectory - ); - return result; - }); + try { + return connectToKiota(context, async (connection) => { + const request = new rpc.RequestType1( + "MigrateFromLockFile" + ); + const result = await connection.sendRequest( + request, + lockFileDirectory + ); + return result; + }); + } catch (error) { + return Promise.resolve(undefined); + } }; export async function checkForLockFileAndPrompt(context: vscode.ExtensionContext) { const workspaceFolders = vscode.workspace.workspaceFolders; - if(workspaceFolders) { - const lockFile = await vscode.workspace.findFiles(`{**/${KIOTA_LOCK_FILE},${KIOTA_LOCK_FILE}}`); + if (workspaceFolders) { + const lockFile = await vscode.workspace.findFiles(`{**/${KIOTA_LOCK_FILE},${KIOTA_LOCK_FILE}}`); - if (lockFile.length > 0) { - const result = await vscode.window.showInformationMessage( - vscode.l10n.t("Please migrate your API clients to Kiota workspace."), - vscode.l10n.t("OK"), - vscode.l10n.t("Remind me later") - ); + if (lockFile.length > 0) { + const result = await vscode.window.showInformationMessage( + vscode.l10n.t("Please migrate your API clients to Kiota workspace."), + vscode.l10n.t("OK"), + vscode.l10n.t("Remind me later") + ); - if (result === vscode.l10n.t("OK")) { - await handleMigration(context, workspaceFolders![0]); - await vscode.commands.executeCommand('kiota.workspace.refresh'); - } - } + if (result === vscode.l10n.t("OK")) { + await handleMigration(context, workspaceFolders![0]); + await vscode.commands.executeCommand('kiota.workspace.refresh'); + } + } } - }; +}; export function displayMigrationMessages(logEntries: KiotaLogEntry[]) { const workspaceJsonUri = vscode.Uri.file(getWorkspaceJsonPath()); - const successEntries = logEntries.filter(entry => + const successEntries = logEntries.filter(entry => entry.level === LogLevel.information && entry.message.includes("migrated successfully") ); diff --git a/vscode/microsoft-kiota/src/openApiTreeProvider.ts b/vscode/microsoft-kiota/src/openApiTreeProvider.ts index f48718c35a..2b4a25758c 100644 --- a/vscode/microsoft-kiota/src/openApiTreeProvider.ts +++ b/vscode/microsoft-kiota/src/openApiTreeProvider.ts @@ -4,18 +4,19 @@ import * as path from 'path'; import * as vscode from 'vscode'; import * as rpc from 'vscode-jsonrpc/node'; import * as crypto from 'crypto'; -import { - ClientObjectProperties, - ClientOrPluginProperties, - connectToKiota, - KiotaGetManifestDetailsConfiguration, - KiotaLogEntry, - KiotaManifestResult, - KiotaOpenApiNode, - KiotaShowConfiguration, - KiotaShowResult, - ConfigurationFile, - PluginObjectProperties } from './kiotaInterop'; +import { + ClientObjectProperties, + ClientOrPluginProperties, + connectToKiota, + KiotaGetManifestDetailsConfiguration, + KiotaLogEntry, + KiotaManifestResult, + KiotaOpenApiNode, + KiotaShowConfiguration, + KiotaShowResult, + ConfigurationFile, + PluginObjectProperties +} from './kiotaInterop'; import { ExtensionSettings } from './extensionSettings'; import { treeViewId } from './constants'; import { updateTreeViewIcons } from './util'; @@ -233,6 +234,9 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider { - const request = new rpc.RequestType('Show'); - return await connection.sendRequest(request, { - includeFilters: this.includeFilters, - excludeFilters: this.excludeFilters, - descriptionPath: this.descriptionUrl, - clearCache + try { + const result = await connectToKiota(this.context, async (connection) => { + const request = new rpc.RequestType('Show'); + return await connection.sendRequest(request, { + includeFilters: this.includeFilters, + excludeFilters: this.excludeFilters, + descriptionPath: this.descriptionUrl, + clearCache + }); }); - }); - if (result) { - this.apiTitle = result.apiTitle; - if (result.rootNode) { - if (this.includeFilters.length === 0) { - this.setAllSelected(result.rootNode, false); - } - this.rawRootNode = result.rootNode; - if (clientNameOrPluginName) { - this.rawRootNode = createKiotaOpenApiNode( - clientNameOrPluginName, - '/', - [this.rawRootNode], - false, - false, - undefined, - clientNameOrPluginName - ); + if (result) { + this.apiTitle = result.apiTitle; + if (result.rootNode) { + if (this.includeFilters.length === 0) { + this.setAllSelected(result.rootNode, false); + } + this.rawRootNode = result.rootNode; + if (clientNameOrPluginName) { + this.rawRootNode = createKiotaOpenApiNode( + clientNameOrPluginName, + '/', + [this.rawRootNode], + false, + false, + undefined, + clientNameOrPluginName + ); + } + await updateTreeViewIcons(treeViewId, true, false); } - await updateTreeViewIcons(treeViewId, true, false); } + } catch (err) { + vscode.window.showErrorMessage((err as Error)?.message || 'An unknown error occurred'); } } getCollapsedState(node: KiotaOpenApiNode): vscode.TreeItemCollapsibleState { diff --git a/vscode/microsoft-kiota/src/searchDescription.ts b/vscode/microsoft-kiota/src/searchDescription.ts index 075d0bd534..6fe76032ba 100644 --- a/vscode/microsoft-kiota/src/searchDescription.ts +++ b/vscode/microsoft-kiota/src/searchDescription.ts @@ -3,6 +3,7 @@ import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; export function searchDescription(context: vscode.ExtensionContext, searchTerm: string, clearCache: boolean): Promise | undefined> { + try { return connectToKiota>(context, async (connection) => { const request = new rpc.RequestType2( "Search" @@ -12,9 +13,12 @@ export function searchDescription(context: vscode.ExtensionContext, searchTerm: searchTerm, clearCache, ); - if(result) { + if (result) { return result.results; } return undefined; }); + } catch (error) { + return Promise.resolve(undefined); + } }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/updateClients.ts b/vscode/microsoft-kiota/src/updateClients.ts index 2ef1c7a4f2..d47f158f1f 100644 --- a/vscode/microsoft-kiota/src/updateClients.ts +++ b/vscode/microsoft-kiota/src/updateClients.ts @@ -3,16 +3,20 @@ import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; export function updateClients(context: vscode.ExtensionContext, cleanOutput: boolean, clearCache: boolean): Promise { + try { return connectToKiota(context, async (connection) => { - const request = new rpc.RequestType3( - "Update" - ); - const result = await connection.sendRequest( - request, - vscode.workspace.workspaceFolders![0].uri.fsPath, - cleanOutput, - clearCache, - ); - return result; - }); + const request = new rpc.RequestType3( + "Update" + ); + const result = await connection.sendRequest( + request, + vscode.workspace.workspaceFolders![0].uri.fsPath, + cleanOutput, + clearCache, + ); + return result; + }); + } catch (error) { + return Promise.resolve(undefined); + } }; \ No newline at end of file