Skip to content

Commit

Permalink
[formrecognizer] include error code for errors that occur during poll…
Browse files Browse the repository at this point in the history
…ing (#12621)

* include error code for errors that occur during polling

* fix error message

* update tests to check for presence of error code/message

* update changelog

* switch to pytest.raises for tests
  • Loading branch information
kristapratico authored Jul 22, 2020
1 parent 550caa8 commit 55985c4
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 55985c4

Please sign in to comment.