diff --git a/src/azure-cli/azure/cli/command_modules/find/custom.py b/src/azure-cli/azure/cli/command_modules/find/custom.py index c40e6aaa0c6..dc21587b7ce 100644 --- a/src/azure-cli/azure/cli/command_modules/find/custom.py +++ b/src/azure-cli/azure/cli/command_modules/find/custom.py @@ -34,7 +34,7 @@ def process_query(cli_term): print(random.choice(WAIT_MESSAGE), file=sys.stderr) response = call_aladdin_service(cli_term) - if response.status_code != 200: + if response is None or response.status_code != 200: logger.error( "The `az find` command has been retired. A new experience is being developed to replace it. " "In the meantime, please use `az --help` to explore commands and examples, " @@ -70,7 +70,7 @@ def get_generated_examples(cli_term): examples = [] response = call_aladdin_service(cli_term) - if response.status_code == 200: + if response is not None and response.status_code == 200: for answer in json.loads(response.content): examples.append(clean_from_http_answer(answer)) @@ -126,14 +126,19 @@ def call_aladdin_service(query): 'X-UserId': hashed_user_id } - response = requests.get( - api_url, - params={ - 'query': query, - 'clientType': 'AzureCli', - 'context': json.dumps(context) - }, - headers=headers) + try: + response = requests.get( + api_url, + params={ + 'query': query, + 'clientType': 'AzureCli', + 'context': json.dumps(context) + }, + headers=headers) + except requests.exceptions.ConnectionError as err: + logger.warning("Failed to connect to 'app.aladdin.microsoft.com'. " + "Please check your network connection. Error detail: %s", err) + return None return response diff --git a/src/azure-cli/azure/cli/command_modules/find/tests/latest/test_find.py b/src/azure-cli/azure/cli/command_modules/find/tests/latest/test_find.py index 0044640213a..6d7b837b03f 100644 --- a/src/azure-cli/azure/cli/command_modules/find/tests/latest/test_find.py +++ b/src/azure-cli/azure/cli/command_modules/find/tests/latest/test_find.py @@ -77,6 +77,16 @@ def test_get_generated_examples_empty(self): self.assertEqual(0, len(examples)) + def test_call_aladdin_service_connection_error(self): + with mock.patch('requests.get', side_effect=requests.exceptions.ConnectionError('Name resolution failed')): + response = call_aladdin_service('vm') + self.assertIsNone(response) + + def test_get_generated_examples_connection_error(self): + with mock.patch('requests.get', side_effect=requests.exceptions.ConnectionError('Name resolution failed')): + examples = get_generated_examples('vm') + self.assertEqual(0, len(examples)) + if __name__ == '__main__': unittest.main()