From 8e2e40819951a73bbcb9f807b6cb56d05f7d9315 Mon Sep 17 00:00:00 2001 From: Jiashuo Li Date: Wed, 22 Jul 2020 11:43:36 +0800 Subject: [PATCH 1/2] Capture HTTPError --- src/azure-cli-core/azure/cli/core/_profile.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 000a53ff3f7..14869709301 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -389,10 +389,17 @@ def find_subscriptions_in_cloud_console(self): def _get_token_from_cloud_shell(self, resource): # pylint: disable=no-self-use from azure.cli.core.adal_authentication import MSIAuthenticationWrapper - auth = MSIAuthenticationWrapper(resource=resource) - auth.set_token() - token_entry = auth.token - return (token_entry['token_type'], token_entry['access_token'], token_entry) + import requests + try: + auth = MSIAuthenticationWrapper(resource=resource) + auth.set_token() + token_entry = auth.token + return (token_entry['token_type'], token_entry['access_token'], token_entry) + except requests.exceptions.HTTPError: + import traceback + msg = "Failed to retrieve a token in Cloud Shell for resource {}. Please run `az login` and try again.\n\n" \ + "{}".format(resource, traceback.format_exc()) + raise CLIError(msg) def _set_subscriptions(self, new_subscriptions, merge=True, secondary_key_name=None): From 020e7576e6783a5f16e30c0b719744da4655056d Mon Sep 17 00:00:00 2001 From: Jiashuo Li Date: Wed, 22 Jul 2020 13:29:49 +0800 Subject: [PATCH 2/2] pylint --- src/azure-cli-core/azure/cli/core/_profile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 14869709301..e6dd59c894e 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -397,8 +397,8 @@ def _get_token_from_cloud_shell(self, resource): # pylint: disable=no-self-use return (token_entry['token_type'], token_entry['access_token'], token_entry) except requests.exceptions.HTTPError: import traceback - msg = "Failed to retrieve a token in Cloud Shell for resource {}. Please run `az login` and try again.\n\n" \ - "{}".format(resource, traceback.format_exc()) + msg = "Failed to retrieve a token in Cloud Shell for resource {}. Please run `az login` and try " \ + "again.\n\n{}".format(resource, traceback.format_exc()) raise CLIError(msg) def _set_subscriptions(self, new_subscriptions, merge=True, secondary_key_name=None):