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

[formrecognizer] include error code for errors that occur during polling #12621

Merged
merged 6 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aligning with other languages to raise the first error in the list

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