Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Display error w/o token on auth failure #6576

Merged
merged 10 commits into from
Mar 31, 2021
19 changes: 16 additions & 3 deletions extensions/azurePublish/src/components/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
/* eslint-disable no-underscore-dangle */
import axios from 'axios';
import formatMessage from 'format-message';
import { SubscriptionClient } from '@azure/arm-subscriptions';
import { Subscription } from '@azure/arm-subscriptions/esm/models';
import { ResourceManagementClient } from '@azure/arm-resources';
Expand All @@ -27,6 +28,11 @@ import * as Images from './images';

const logger = debug('composer:extension:azureProvision');

/**
* Retrieves the list of subscriptions from Azure
* @param token The authentication token
* @returns The list of subscriptions or throws
*/
export const getSubscriptions = async (token: string): Promise<Array<Subscription>> => {
const tokenCredentials = new TokenCredentials(token);
try {
Expand All @@ -37,15 +43,22 @@ export const getSubscriptions = async (token: string): Promise<Array<Subscriptio
status: AzureAPIStatus.ERROR,
message: subscriptionsResult._response.bodyAsText,
});
return [];
throw new Error(subscriptionsResult._response.bodyAsText);
}
return subscriptionsResult._response.parsedBody;
} catch (err) {
let message = JSON.stringify(err, Object.getOwnPropertyNames(err));
if (err?.code === 12 && err?.message?.match(/Bearer/gi)) {
message = formatMessage(
'There was an authentication problem retrieving subscriptions. Verify your login session has not expired and you have permission to list subscriptions in this account.'
);
}

logger({
status: AzureAPIStatus.ERROR,
message: JSON.stringify(err, Object.getOwnPropertyNames(err)),
message,
});
return [];
throw new Error(message);
}
};

Expand Down
34 changes: 25 additions & 9 deletions extensions/azurePublish/src/components/azureProvisionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export const AzureProvisionDialog: React.FC = () => {
const extensionState = { ...defaultExtensionState, ...getItem(profileName) };

const [subscriptions, setSubscriptions] = useState<Subscription[] | undefined>();
const [subscriptionsErrorMessage, setSubscriptionsErrorMessage] = useState<string>();
const [deployLocations, setDeployLocations] = useState<DeployLocation[]>([]);
const [luisLocations, setLuisLocations] = useState<DeployLocation[]>([]);

Expand Down Expand Up @@ -356,11 +357,24 @@ export const AzureProvisionDialog: React.FC = () => {

useEffect(() => {
if (token) {
getSubscriptions(token).then((data) => {
if (isMounted.current) {
setSubscriptions(data);
}
});
setSubscriptionsErrorMessage(undefined);
getSubscriptions(token)
.then((data) => {
if (isMounted.current) {
setSubscriptions(data);
if (data.length === 0) {
setSubscriptionsErrorMessage(
formatMessage(
'Your subscription list is empty, please add your subscription, or login with another account.'
)
);
}
}
})
.catch((err) => {
setSubscriptionsErrorMessage(err.message);
GeoffCoxMSFT marked this conversation as resolved.
Show resolved Hide resolved
});

getResources();
}
}, [token]);
Expand Down Expand Up @@ -657,10 +671,12 @@ export const AzureProvisionDialog: React.FC = () => {
)}
</form>
)}
{choice.key === 'create' && loginErrorMsg !== '' && <div style={{ marginTop: '10px' }}> {loginErrorMsg} </div>}
{choice.key === 'create' && currentUser && subscriptionOption?.length < 1 && (
<div style={{ marginTop: '10px' }}>
Your subscription list is empty, please add your subscription, or login with another account.
{choice.key === 'create' && loginErrorMsg !== '' && (
<div style={{ marginTop: '10px', color: FluentTheme.semanticColors.errorText }}> {loginErrorMsg} </div>
)}
{choice.key === 'create' && currentUser && subscriptionsErrorMessage && (
<div style={{ marginTop: '10px', color: FluentTheme.semanticColors.errorText }}>
{subscriptionsErrorMessage}
</div>
)}
</Suspense>
Expand Down