Skip to content

Commit

Permalink
Merge pull request #5416 from microsoft/chore/linting
Browse files Browse the repository at this point in the history
Fix: Proper error handling in connectToKiota calls
  • Loading branch information
baywet authored Sep 30, 2024
2 parents 112bbf7 + ec63eac commit 9e44119
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 201 deletions.
58 changes: 27 additions & 31 deletions vscode/microsoft-kiota/src/generateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,31 @@ export function generateClient(context: vscode.ExtensionContext,
operation: ConsumerOperation,
workingDirectory: string = getWorkspaceJsonDirectory()
): Promise<KiotaLogEntry[] | undefined> {
try {
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"Generate"
);
return await connection.sendRequest(
request,
{
cleanOutput: cleanOutput,
clearCache: clearCache,
clientClassName: clientClassName,
clientNamespaceName: clientNamespaceName,
deserializers: deserializers,
disabledValidationRules: disableValidationRules,
excludeBackwardCompatible: excludeBackwardCompatible,
excludePatterns: excludeFilters,
includeAdditionalData: includeAdditionalData,
includePatterns: includeFilters,
language: language,
openAPIFilePath: descriptionPath,
outputPath: output,
serializers: serializers,
structuredMimeTypes: structuredMimeTypes,
usesBackingStore: usesBackingStore,
operation: operation
} as GenerationConfiguration,
);
}, workingDirectory);
} catch (error) {
return Promise.resolve(undefined);
}
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"Generate"
);
return await connection.sendRequest(
request,
{
cleanOutput: cleanOutput,
clearCache: clearCache,
clientClassName: clientClassName,
clientNamespaceName: clientNamespaceName,
deserializers: deserializers,
disabledValidationRules: disableValidationRules,
excludeBackwardCompatible: excludeBackwardCompatible,
excludePatterns: excludeFilters,
includeAdditionalData: includeAdditionalData,
includePatterns: includeFilters,
language: language,
openAPIFilePath: descriptionPath,
outputPath: output,
serializers: serializers,
structuredMimeTypes: structuredMimeTypes,
usesBackingStore: usesBackingStore,
operation: operation
} as GenerationConfiguration,
);
}, workingDirectory);
};
44 changes: 20 additions & 24 deletions vscode/microsoft-kiota/src/generatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@ export function generatePlugin(context: vscode.ExtensionContext,
disableValidationRules: string[],
operation: ConsumerOperation,
workingDirectory: string = getWorkspaceJsonDirectory()): Promise<KiotaLogEntry[] | undefined> {
try {
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"GeneratePlugin"
);
return await connection.sendRequest(
request,
{
pluginTypes: pluginTypes,
cleanOutput: cleanOutput,
clearCache: clearCache,
clientClassName: clientClassName,
disabledValidationRules: disableValidationRules,
excludePatterns: excludeFilters,
includePatterns: includeFilters,
openAPIFilePath: descriptionPath,
outputPath: output,
operation: operation
} as GenerationConfiguration,
);
}, workingDirectory);
} catch (error) {
return Promise.resolve(undefined);
}
return connectToKiota<KiotaLogEntry[]>(context, async (connection) => {
const request = new rpc.RequestType1<GenerationConfiguration, KiotaLogEntry[], void>(
"GeneratePlugin"
);
return await connection.sendRequest(
request,
{
pluginTypes: pluginTypes,
cleanOutput: cleanOutput,
clearCache: clearCache,
clientClassName: clientClassName,
disabledValidationRules: disableValidationRules,
excludePatterns: excludeFilters,
includePatterns: includeFilters,
openAPIFilePath: descriptionPath,
outputPath: output,
operation: operation
} as GenerationConfiguration,
);
}, workingDirectory);
};
30 changes: 13 additions & 17 deletions vscode/microsoft-kiota/src/getKiotaVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ import * as rpc from "vscode-jsonrpc/node";
import * as vscode from "vscode";

export function getKiotaVersion(context: vscode.ExtensionContext, kiotaOutputChannel: vscode.LogOutputChannel): Promise<string | undefined> {
try {
return connectToKiota<string>(context, async (connection) => {
const request = new rpc.RequestType0<string, void>("GetVersion");
const result = await connection.sendRequest(request);
if (result) {
const version = result.split("+")[0];
if (version) {
kiotaOutputChannel.info(`kiota: ${version}`);
return version;
}
return connectToKiota<string>(context, async (connection) => {
const request = new rpc.RequestType0<string, void>("GetVersion");
const result = await connection.sendRequest(request);
if (result) {
const version = result.split("+")[0];
if (version) {
kiotaOutputChannel.info(`kiota: ${version}`);
return version;
}
kiotaOutputChannel.error(`kiota: ${vscode.l10n.t('not found')}`);
kiotaOutputChannel.show();
return undefined;
});
} catch (error) {
return Promise.resolve(undefined);
}
}
kiotaOutputChannel.error(`kiota: ${vscode.l10n.t('not found')}`);
kiotaOutputChannel.show();
return undefined;
});
};
44 changes: 18 additions & 26 deletions vscode/microsoft-kiota/src/getLanguageInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,25 @@ export async function getLanguageInformation(context: vscode.ExtensionContext):
};

function getLanguageInformationInternal(context: vscode.ExtensionContext): Promise<LanguagesInformation | undefined> {
try {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType0<LanguagesInformation, void>(
"Info"
);
return await connection.sendRequest(
request,
);
});
} catch (error) {
return Promise.resolve(undefined);
}
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType0<LanguagesInformation, void>(
"Info"
);
return await connection.sendRequest(
request,
);
});
};

export function getLanguageInformationForDescription(context: vscode.ExtensionContext, descriptionUrl: string, clearCache: boolean): Promise<LanguagesInformation | undefined> {
try {
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, LanguagesInformation, void>(
"InfoForDescription"
);
return await connection.sendRequest(
request,
descriptionUrl,
clearCache
);
});
} catch (error) {
return Promise.resolve(undefined);
}
return connectToKiota<LanguagesInformation>(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, LanguagesInformation, void>(
"InfoForDescription"
);
return await connection.sendRequest(
request,
descriptionUrl,
clearCache
);
});
};
46 changes: 23 additions & 23 deletions vscode/microsoft-kiota/src/kiotaInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function connectToKiota<T>(context: vscode.ExtensionContext, callba
} catch (error) {
const errorMessage = (error as { data?: { message: string } })?.data?.message
|| 'An unknown error occurred';
throw new Error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
} finally {
connection.dispose();
childProcess.kill();
Expand Down Expand Up @@ -197,30 +197,30 @@ export function maturityLevelToString(level: MaturityLevel): string {
}
}
export enum DependencyType {
abstractions,
serialization,
authentication,
http,
bundle,
additional,
abstractions,
serialization,
authentication,
http,
bundle,
additional,
}
export function dependencyTypeToString(type: DependencyType): string {
switch (type) {
case DependencyType.abstractions:
return "abstractions";
case DependencyType.serialization:
return "serialization";
case DependencyType.authentication:
return "authentication";
case DependencyType.http:
return "http";
case DependencyType.bundle:
return "bundle";
case DependencyType.additional:
return "additional";
default:
throw new Error("unknown type");
}
switch (type) {
case DependencyType.abstractions:
return "abstractions";
case DependencyType.serialization:
return "serialization";
case DependencyType.authentication:
return "authentication";
case DependencyType.http:
return "http";
case DependencyType.bundle:
return "bundle";
case DependencyType.additional:
return "additional";
default:
throw new Error("unknown type");
}
}
export interface ConfigurationFile {
version: string;
Expand Down
24 changes: 10 additions & 14 deletions vscode/microsoft-kiota/src/migrateFromLockFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ import { KIOTA_LOCK_FILE } from "./constants";
import { getWorkspaceJsonPath, handleMigration } from "./util";

export function migrateFromLockFile(context: vscode.ExtensionContext, lockFileDirectory: string): Promise<KiotaLogEntry[] | undefined> {
try {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType1<string, KiotaLogEntry[], void>(
"MigrateFromLockFile"
);
const result = await connection.sendRequest(
request,
lockFileDirectory
);
return result;
});
} catch (error) {
return Promise.resolve(undefined);
}
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType1<string, KiotaLogEntry[], void>(
"MigrateFromLockFile"
);
const result = await connection.sendRequest(
request,
lockFileDirectory
);
return result;
});
};

export async function checkForLockFileAndPrompt(context: vscode.ExtensionContext) {
Expand Down
60 changes: 28 additions & 32 deletions vscode/microsoft-kiota/src/openApiTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
if (!this.rawRootNode) {
return;
}
this.tokenizedFilter = filterText.length === 0 ? [] : filterText.split(' ').filter(x => x !== '').map(x => x.trim().toLowerCase()).sort();
this.tokenizedFilter = filterText.length === 0 ? [] : filterText.split(' ').filter(x => x !== '').map(x => x.trim().toLowerCase()).sort((a, b) => a.localeCompare(b));
this.refreshView();
}
public get filter(): string {
Expand Down Expand Up @@ -320,40 +320,36 @@ export class OpenApiTreeProvider implements vscode.TreeDataProvider<OpenApiTreeN
if (!this.descriptionUrl || this.descriptionUrl.length === 0) {
return;
}
try {
const result = await connectToKiota(this.context, async (connection) => {
const request = new rpc.RequestType<KiotaShowConfiguration, KiotaShowResult, void>('Show');
return await connection.sendRequest(request, {
includeFilters: this.includeFilters,
excludeFilters: this.excludeFilters,
descriptionPath: this.descriptionUrl,
clearCache
});
const result = await connectToKiota(this.context, async (connection) => {
const request = new rpc.RequestType<KiotaShowConfiguration, KiotaShowResult, void>('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
);
}
await updateTreeViewIcons(treeViewId, true, false);
});
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
);
}
void vscode.window.showInformationMessage(vscode.l10n.t('You can now select the required endpoints from {0}', this.apiTitle!));
await updateTreeViewIcons(treeViewId, true, false);
}
} catch (err) {
vscode.window.showErrorMessage((err as Error)?.message || 'An unknown error occurred');
void vscode.window.showInformationMessage(vscode.l10n.t('You can now select the required endpoints from {0}', this.apiTitle!));
}
}
getCollapsedState(node: KiotaOpenApiNode): vscode.TreeItemCollapsibleState {
Expand Down
Loading

0 comments on commit 9e44119

Please sign in to comment.