Skip to content

Commit fa07ab1

Browse files
committed
Use an enum for error codes
1 parent 29e7976 commit fa07ab1

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

api/docs/vscode-azureresources-api.d.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,42 +233,35 @@ export declare interface AzureResourcesApiRequestContext {
233233
onApiRequestError?: (error: AzureResourcesApiRequestError) => void | Promise<void>;
234234
}
235235

236-
export declare type AzureResourcesApiRequestError = typeof AzureResourcesApiRequestErrors[keyof typeof AzureResourcesApiRequestErrors] & {
236+
export declare type AzureResourcesApiRequestError = {
237+
code: AzureResourcesApiRequestErrorCode;
237238
message: string;
238239
};
239240

240241
/**
241-
* List of errors that could occur during the authentication handshake between client extension and Azure Resources host extension.
242+
* Error codes that could appear during the API request handshake between client extension and Azure Resources host extension.
242243
*/
243-
export declare const AzureResourcesApiRequestErrors: {
244+
export declare enum AzureResourcesApiRequestErrorCode {
244245
/**
245246
* An error occurred while the client extension was creating its verification credential for the Azure Resources host extension.
246247
*/
247-
readonly CLIENT_FAILED_CREATE_CREDENTIAL: {
248-
readonly code: "ERR_CLIENT_FAILED_CREATE_CREDENTIAL";
249-
};
248+
CLIENT_FAILED_CREATE_CREDENTIAL = "ERR_CLIENT_FAILED_CREATE_CREDENTIAL",
250249
/**
251250
* An error occurred while the Azure Resources host extension was trying to create an API session.
252251
*/
253-
readonly HOST_CREATE_SESSION_FAILED: {
254-
readonly code: "ERR_HOST_CREATE_SESSION_FAILED";
255-
};
252+
HOST_CREATE_SESSION_FAILED = "ERR_HOST_CREATE_SESSION_FAILED",
256253
/**
257254
* An error occurred because the client's receiver method was provided incomplete or missing credentials.
258255
*/
259-
readonly CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS: {
260-
readonly code: "ERR_CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS";
261-
};
256+
CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS = "ERR_CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS",
262257
/**
263258
* The client's receiver method was provided a client credential that failed verification.
264259
*
265260
* This may occur when:
266261
* - An untrusted extension pretends to be the Azure Resources host extension and tries to pass a fake credential
267262
* - There is a faulty behavior in the client's verification process
268263
*/
269-
readonly CLIENT_CREDENTIAL_FAILED_VERIFICATION: {
270-
readonly code: "ERR_CLIENT_CREDENTIAL_FAILED_VERIFICATION";
271-
};
264+
CLIENT_CREDENTIAL_FAILED_VERIFICATION = "ERR_CLIENT_CREDENTIAL_FAILED_VERIFICATION",
272265
/**
273266
* An error occurred while asking the Azure Resources host extension to provision the specified APIs.
274267
*
@@ -277,10 +270,8 @@ export declare const AzureResourcesApiRequestErrors: {
277270
* - The requesting extension is not on the Azure Resources allow list
278271
* - The host extension encounters an internal error during API provisioning
279272
*/
280-
readonly HOST_API_PROVISIONING_FAILED: {
281-
readonly code: "ERR_HOST_API_PROVISIONING_FAILED";
282-
};
283-
};
273+
HOST_API_PROVISIONING_FAILED = "ERR_HOST_API_PROVISIONING_FAILED"
274+
}
284275

285276
export declare type AzureResourcesApiRequestPrep<T extends AzureExtensionApi> = {
286277
/**

api/src/auth/apiRequest/apiRequest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { apiUtils, AzureExtensionApi } from "../../utils/apiUtils";
99
import { AzExtCredentialManager } from "../credentialManager/AzExtCredentialManager";
1010
import { AzExtUUIDCredentialManager } from "../credentialManager/AzExtUUIDCredentialManager";
1111
import { AzureResourcesApiRequestContext } from "./AzureResourcesApiRequestContext";
12-
import { AzureResourcesApiRequestErrors } from "./apiRequestErrors";
12+
import { AzureResourcesApiRequestErrorCode } from "./apiRequestErrors";
1313

1414
const azureResourcesAuthApiVersion: string = '^4.0.0';
1515
const azureResourcesExtId = 'ms-azuretools.vscode-azureresourcegroups';
@@ -56,7 +56,7 @@ async function requestAzureResourcesSession(context: AzureResourcesApiRequestCon
5656
clientCredential = await clientCredentialManager.createCredential(context.clientExtensionId);
5757
} catch (err) {
5858
if (err instanceof Error) {
59-
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrors.CLIENT_FAILED_CREATE_CREDENTIAL.code, message: clientCredentialManager.maskCredentials(err.message) })
59+
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrorCode.CLIENT_FAILED_CREATE_CREDENTIAL, message: clientCredentialManager.maskCredentials(err.message) })
6060
}
6161
return;
6262
}
@@ -66,7 +66,7 @@ async function requestAzureResourcesSession(context: AzureResourcesApiRequestCon
6666
await resourcesApi.createAzureResourcesApiSession(context.clientExtensionId, clientApiVersion, clientCredential);
6767
} catch (err) {
6868
if (err instanceof Error) {
69-
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrors.HOST_CREATE_SESSION_FAILED.code, message: clientCredentialManager.maskCredentials(err.message) });
69+
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrorCode.HOST_CREATE_SESSION_FAILED, message: clientCredentialManager.maskCredentials(err.message) });
7070
}
7171
return;
7272
}
@@ -75,7 +75,7 @@ async function requestAzureResourcesSession(context: AzureResourcesApiRequestCon
7575
function createReceiveAzureResourcesApiSession(context: AzureResourcesApiRequestContext, clientCredentialManager: AzExtCredentialManager): AzureExtensionApi['receiveAzureResourcesApiSession'] {
7676
return async function (azureResourcesCredential: string, clientCredential: string): Promise<void> {
7777
if (!azureResourcesCredential || !clientCredential) {
78-
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrors.CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS.code, message: 'Insufficient credentials were provided back to the client.' });
78+
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrorCode.CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS, message: 'Insufficient credentials were provided back to the client.' });
7979
return;
8080
}
8181

@@ -86,7 +86,7 @@ function createReceiveAzureResourcesApiSession(context: AzureResourcesApiRequest
8686
}
8787
} catch (err) {
8888
if (err instanceof Error) {
89-
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrors.CLIENT_CREDENTIAL_FAILED_VERIFICATION.code, message: clientCredentialManager.maskCredentials(err.message) });
89+
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrorCode.CLIENT_CREDENTIAL_FAILED_VERIFICATION, message: clientCredentialManager.maskCredentials(err.message) });
9090
}
9191
return;
9292
}
@@ -97,7 +97,7 @@ function createReceiveAzureResourcesApiSession(context: AzureResourcesApiRequest
9797
void context.onDidReceiveAzureResourcesApis(resourcesApis);
9898
} catch (err) {
9999
if (err instanceof Error) {
100-
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrors.HOST_API_PROVISIONING_FAILED.code, message: clientCredentialManager.maskCredentials(err.message) });
100+
void context.onApiRequestError?.({ code: AzureResourcesApiRequestErrorCode.HOST_API_PROVISIONING_FAILED, message: clientCredentialManager.maskCredentials(err.message) });
101101
}
102102
return;
103103
}

api/src/auth/apiRequest/apiRequestErrors.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
/**
7-
* List of errors that could occur during the authentication handshake between client extension and Azure Resources host extension.
7+
* Error codes that could appear during the API request handshake between client extension and Azure Resources host extension.
88
*/
9-
export const AzureResourcesApiRequestErrors = {
10-
9+
export enum AzureResourcesApiRequestErrorCode {
1110
/**
1211
* An error occurred while the client extension was creating its verification credential for the Azure Resources host extension.
1312
*/
14-
CLIENT_FAILED_CREATE_CREDENTIAL: { code: 'ERR_CLIENT_FAILED_CREATE_CREDENTIAL' },
13+
CLIENT_FAILED_CREATE_CREDENTIAL = 'ERR_CLIENT_FAILED_CREATE_CREDENTIAL',
1514

1615
/**
1716
* An error occurred while the Azure Resources host extension was trying to create an API session.
1817
*/
19-
HOST_CREATE_SESSION_FAILED: { code: 'ERR_HOST_CREATE_SESSION_FAILED' },
18+
HOST_CREATE_SESSION_FAILED = 'ERR_HOST_CREATE_SESSION_FAILED',
2019

2120
/**
2221
* An error occurred because the client's receiver method was provided incomplete or missing credentials.
2322
*/
24-
CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS: { code: 'ERR_CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS' },
23+
CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS = 'ERR_CLIENT_RECEIVED_INSUFFICIENT_CREDENTIALS',
2524

2625
/**
2726
* The client's receiver method was provided a client credential that failed verification.
@@ -30,7 +29,7 @@ export const AzureResourcesApiRequestErrors = {
3029
* - An untrusted extension pretends to be the Azure Resources host extension and tries to pass a fake credential
3130
* - There is a faulty behavior in the client's verification process
3231
*/
33-
CLIENT_CREDENTIAL_FAILED_VERIFICATION: { code: 'ERR_CLIENT_CREDENTIAL_FAILED_VERIFICATION' },
32+
CLIENT_CREDENTIAL_FAILED_VERIFICATION = 'ERR_CLIENT_CREDENTIAL_FAILED_VERIFICATION',
3433

3534
/**
3635
* An error occurred while asking the Azure Resources host extension to provision the specified APIs.
@@ -40,8 +39,7 @@ export const AzureResourcesApiRequestErrors = {
4039
* - The requesting extension is not on the Azure Resources allow list
4140
* - The host extension encounters an internal error during API provisioning
4241
*/
43-
HOST_API_PROVISIONING_FAILED: { code: 'ERR_HOST_API_PROVISIONING_FAILED' },
44-
45-
} as const satisfies Record<string, { code: string }>;
42+
HOST_API_PROVISIONING_FAILED = 'ERR_HOST_API_PROVISIONING_FAILED',
43+
}
4644

47-
export type AzureResourcesApiRequestError = typeof AzureResourcesApiRequestErrors[keyof typeof AzureResourcesApiRequestErrors] & { message: string };
45+
export type AzureResourcesApiRequestError = { code: AzureResourcesApiRequestErrorCode, message: string };

api/src/auth/credentialManager/AzExtUUIDCredentialManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ import { AzExtCredentialManager } from "./AzExtCredentialManager";
1111
* A simple, light-weight credential manager that issues and tracks randomly generated UUIDs for extension verification.
1212
*/
1313
export class AzExtUUIDCredentialManager implements AzExtCredentialManager {
14-
protected _uuidMap: Map<string, string> = new Map();
14+
#uuidMap: Map<string, string> = new Map();
1515

1616
createCredential(extensionId: string): string {
1717
const uuid: string = crypto.randomUUID();
18-
this._uuidMap.set(extensionId, uuid);
18+
this.#uuidMap.set(extensionId, uuid);
1919
return uuid;
2020
}
2121

2222
verifyCredential(credential: string, extensionId: string): boolean {
2323
if (!credential || !extensionId) {
2424
return false;
2525
}
26-
return credential === this._uuidMap.get(extensionId);
26+
return credential === this.#uuidMap.get(extensionId);
2727
}
2828

2929
maskCredentials(data: string): string {
30-
for (const uuid of this._uuidMap.values()) {
30+
for (const uuid of this.#uuidMap.values()) {
3131
data = maskValue(data, uuid);
3232
}
3333
return data;

0 commit comments

Comments
 (0)