diff --git a/.eslintignore b/.eslintignore index 4bb33f1bd5..25ea1e033f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,6 +3,7 @@ **/extensions/**/*.d.ts **/extensions/**/build/** **/extensions/**/colorize-fixtures/** +**/extensions/azurecore/extension.webpack.config.js **/extensions/css-language-features/server/test/pathCompletionFixtures/** **/extensions/html-language-features/server/lib/jquery.d.ts **/extensions/html-language-features/server/src/test/pathCompletionFixtures/** diff --git a/extensions/azurecore/.eslintrc.json b/extensions/azurecore/.eslintrc.json index 74218d068e..201f6cc862 100644 --- a/extensions/azurecore/.eslintrc.json +++ b/extensions/azurecore/.eslintrc.json @@ -11,6 +11,7 @@ } ], // Disabled until the issues can be fixed - "@typescript-eslint/explicit-function-return-type": ["off"] + "@typescript-eslint/explicit-function-return-type": ["off"], + "@typescript-eslint/no-unsafe-assignment": "error" } } diff --git a/extensions/azurecore/src/account-provider/auths/azureAuth.ts b/extensions/azurecore/src/account-provider/auths/azureAuth.ts index 2db8d524a4..1f305114cd 100644 --- a/extensions/azurecore/src/account-provider/auths/azureAuth.ts +++ b/extensions/azurecore/src/account-provider/auths/azureAuth.ts @@ -29,6 +29,7 @@ import { HttpClient } from './httpClient'; import { getProxyEnabledHttpClient, getTenantIgnoreList, updateTenantIgnoreList } from '../../utils'; import { errorToPromptFailedResult } from './networkUtils'; import { MsalCachePluginProvider } from '../utils/msalCachePlugin'; +import { AzureListOperationResponse, ErrorResponseBodyWithError, isErrorResponseBody as isErrorResponseBodyWithError } from '../../azureResource/utils'; const localize = nls.loadMessageBundle(); export abstract class AzureAuth implements vscode.Disposable { @@ -482,7 +483,7 @@ export abstract class AzureAuth implements vscode.Disposable { Logger.verbose(`Fetching tenants with uri: ${tenantUri}`); let tenantList: string[] = []; - const tenantResponse = await this.httpClient.sendGetRequestAsync(tenantUri, { + const tenantResponse = await this.httpClient.sendGetRequestAsync | ErrorResponseBodyWithError>(tenantUri, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` @@ -490,9 +491,9 @@ export abstract class AzureAuth implements vscode.Disposable { }); const data = tenantResponse.data; - if (data.error) { - Logger.error(`Error fetching tenants :${data.error.code} - ${data.error.message}`); - throw new Error(`${data.error.code} - ${data.error.message}`); + if (isErrorResponseBodyWithError(data)) { + Logger.error(`Error fetching tenants :${data.error?.code} - ${data.error?.message}`); + throw new Error(`${data.error?.code} - ${data.error?.message}`); } const tenants: Tenant[] = data.value.map((tenantInfo: TenantResponse) => { if (tenantInfo.displayName) { diff --git a/extensions/azurecore/src/account-provider/auths/httpClient.ts b/extensions/azurecore/src/account-provider/auths/httpClient.ts index 78a7282547..48a27dcd28 100644 --- a/extensions/azurecore/src/account-provider/auths/httpClient.ts +++ b/extensions/azurecore/src/account-provider/auths/httpClient.ts @@ -233,10 +233,14 @@ const networkRequestViaProxy = ( // check if the value of the header is supposed to be a JSON object try { + // TODO: Investigate this - https://github.com/microsoft/azuredatastudio/issues/22835 + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const object = JSON.parse(headerValue); // if it is, then convert it from a string to a JSON object if (object && (typeof object === 'object')) { + // TODO: Investigate this - https://github.com/microsoft/azuredatastudio/issues/22835 + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment headerValue = object; } } catch (e) { diff --git a/extensions/azurecore/src/azureResource/utils.ts b/extensions/azurecore/src/azureResource/utils.ts index d4e9c277bc..a195f99b7e 100644 --- a/extensions/azurecore/src/azureResource/utils.ts +++ b/extensions/azurecore/src/azureResource/utils.ts @@ -20,14 +20,33 @@ import * as Constants from '../constants'; import { getProxyEnabledHttpClient } from '../utils'; import { HttpClient } from '../account-provider/auths/httpClient'; import { NetworkRequestOptions } from '@azure/msal-common'; +import { ErrorResponseBody } from '@azure/arm-subscriptions/esm/models'; const localize = nls.loadMessageBundle(); +/** + * Specialized version of the ErrorResponseBody that is required to have the error + * information for easier typing support, without it how do we know it's an error + * response? + * https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/subscription/arm-subscriptions/src/models/index.ts#L180 + */ +export type ErrorResponseBodyWithError = Required; + +/** + * Checks if the body object given is an error response, that is has a non-undefined + * property named error which contains detailed about the error. + * @param body The body object to check + * @returns True if the body is an ErrorResponseBodyWithError + */ +export function isErrorResponseBody(body: any): body is ErrorResponseBodyWithError { + return 'error' in body && body.error; +} + /** * Shape of list operation responses * e.g. https://learn.microsoft.com/en-us/rest/api/storagerp/srp_json_list_operations#response-body */ -declare type AzureListOperationResponse = { +export declare type AzureListOperationResponse = { value: T; }