diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index 96019966b6ef..5cff3433a4e9 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -10,6 +10,11 @@ - `FormField` now has attribute `type` which contains the semantic data type of the field value +**Fixes and improvements** + +- Fixes a bug where error code and message weren't being returned on `HttpResponseError` if operation failed during polling + + ## 1.0.0b4 (2020-07-07) **Breaking Changes** diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_polling.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_polling.py index f1ef5267a283..dcd915789ce0 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_polling.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_polling.py @@ -5,7 +5,7 @@ # ------------------------------------ from typing import TYPE_CHECKING -from azure.core.exceptions import HttpResponseError +from azure.core.exceptions import HttpResponseError, ODataV4Format from azure.core.polling.base_polling import ( LocationPolling, OperationResourcePolling, @@ -18,9 +18,10 @@ def raise_error(response, errors, message): - for err in errors: - message += "({}) {}\n".format(err["code"], err["message"]) - raise HttpResponseError(message=message, response=response) + error_message = "({}) {}{}".format(errors[0]["code"], errors[0]["message"], message) + error = HttpResponseError(message=error_message, response=response) + error.error = ODataV4Format(errors[0]) + raise error class TrainingPolling(LocationPolling): @@ -51,7 +52,7 @@ def get_status(self, pipeline_response): # pylint: disable=no-self-use if train_result: errors = train_result.get("errors") if errors: - message = "Invalid model created with ID={}\n".format(body["modelInfo"]["modelId"]) + message = "\nInvalid model created with ID={}".format(body["modelInfo"]["modelId"]) raise_error(response, errors, message) return "Failed" if status.lower() != "creating": diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py index 08073e8b3c7b..12614c9dc58a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py @@ -62,9 +62,11 @@ def test_copy_model_fail(self, client, container_sas_url, location, resource_id) # give an incorrect region target = client.get_copy_authorization(resource_region="eastus", resource_id=resource_id) - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = client.begin_copy_model(model.model_id, target=target) copy = poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() @GlobalClientPreparer(training=True, copy=True) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py index cd0ee6cb8ff3..58e7fa1e2796 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py @@ -63,9 +63,11 @@ async def test_copy_model_fail(self, client, container_sas_url, location, resour # give an incorrect region target = await client.get_copy_authorization(resource_region="eastus", resource_id=resource_id) - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = await client.begin_copy_model(model.model_id, target=target) copy = await poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() @GlobalClientPreparer(training=True, copy=True) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py index d132c934c6ee..971acdf34bad 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py @@ -81,12 +81,14 @@ def test_custom_form_bad_url(self, client, container_sas_url): poller = client.begin_training(container_sas_url, use_training_labels=True) model = poller.result() - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = fr_client.begin_recognize_custom_forms_from_url( model.model_id, form_url="https://badurl.jpg" ) form = poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() @GlobalClientPreparer(training=True) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py index 010c3a07449f..083a05e41d12 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py @@ -62,9 +62,11 @@ async def test_url_authentication_bad_key(self, resource_group, location, form_r async def test_passing_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = await client.begin_recognize_custom_forms_from_url(model_id="xx", form_url="https://badurl.jpg") result = await poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() async def test_pass_stream_into_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py index 93d255169d73..10b016cb0e46 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py @@ -237,9 +237,11 @@ def test_training_with_files_filter(self, client, container_sas_url): self.assertEqual(len(model.training_documents), 1) self.assertEqual(model.training_documents[0].document_name, "subfolder/Form_6.jpg") # we filtered for only subfolders - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = client.begin_training(training_files_url=container_sas_url, use_training_labels=False, prefix="xxx") model = poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() @GlobalClientPreparer(training=True) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py index 5ab3d350e920..4a0d3a51111b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py @@ -243,9 +243,11 @@ async def test_training_with_files_filter(self, client, container_sas_url): self.assertEqual(len(model.training_documents), 1) self.assertEqual(model.training_documents[0].document_name, "subfolder/Form_6.jpg") # we filtered for only subfolders - with self.assertRaises(HttpResponseError): + with pytest.raises(HttpResponseError) as e: poller = await client.begin_training(training_files_url=container_sas_url, use_training_labels=False, prefix="xxx") model = await poller.result() + self.assertIsNotNone(e.value.error.code) + self.assertIsNotNone(e.value.error.message) @GlobalFormRecognizerAccountPreparer() @GlobalClientPreparer(training=True)