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

[textanalytics] Refactor exceptions to use azure-core ODataV4Format #9934

Merged
merged 7 commits into from
Mar 9, 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
3 changes: 3 additions & 0 deletions sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
**New features**
- Pass `country_hint="none"` to not use the default country hint of `"US"`.

**Dependency updates**
- Adopted [azure-core](https://pypi.org/project/azure-core/) version 1.3.0 or greater

## 1.0.0b2 (2020-02-11)

**Breaking changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
# ------------------------------------

import json
from azure.core.pipeline.policies import ContentDecodePolicy
from azure.core.exceptions import (
HttpResponseError,
ClientAuthenticationError,
DecodeError,
ODataV4Format
)
from ._models import (
RecognizeEntitiesResult,
Expand All @@ -30,41 +29,24 @@
)


class CSODataV4Format(ODataV4Format):
INNERERROR_LABEL = "innerError" # Service plans to fix casing ("innererror") to reflect ODataV4 error spec

def __init__(self, odata_error):
try:
if odata_error["error"]["innerError"]:
super(CSODataV4Format, self).__init__(odata_error["error"]["innerError"])
except KeyError:
super(CSODataV4Format, self).__init__(odata_error)


def process_batch_error(error):
"""Raise detailed error message for HttpResponseErrors
"""Raise detailed error message.
"""
raise_error = HttpResponseError
if error.status_code == 401:
raise_error = ClientAuthenticationError
error_message = error.message
error_code = error.status_code
error_body, error_target = None, None

try:
error_body = ContentDecodePolicy.deserialize_from_http_generics(error.response)
except DecodeError:
pass

try:
if error_body is not None:
error_resp = error_body["error"]
if "innerError" in error_resp:
error_resp = error_resp["innerError"]

error_message = error_resp["message"]
error_code = error_resp["code"]
error_target = error_resp.get("target", None)
if error_target:
error_message += "\nErrorCode:{}\nTarget:{}".format(error_code, error_target)
else:
error_message += "\nErrorCode:{}".format(error_code)
except KeyError:
raise HttpResponseError(message="There was an unknown error with the request.")

error = raise_error(message=error_message, response=error.response)
error.error_code = error_code
error.target = error_target
raise error
raise raise_error(response=error.response, error_format=CSODataV4Format)


def order_results(response, combined):
Expand Down
2 changes: 1 addition & 1 deletion sdk/textanalytics/azure-ai-textanalytics/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
'azure.ai',
]),
install_requires=[
"azure-core<2.0.0,>=1.1.0",
"azure-core<2.0.0,>=1.3.0",
"msrest>=0.6.0",
'azure-common~=1.1',
'six>=1.6',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ def test_bad_model_version_error(self, resource_group, location, text_analytics_
try:
result = text_analytics.analyze_sentiment(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_document_errors(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down Expand Up @@ -458,8 +458,8 @@ def test_missing_input_records_error(self, resource_group, location, text_analyt
try:
result = text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_duplicate_ids_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -470,8 +470,8 @@ def test_duplicate_ids_error(self, resource_group, location, text_analytics_acco
try:
result = text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_batch_size_over_limit_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -482,8 +482,8 @@ def test_batch_size_over_limit_error(self, resource_group, location, text_analyt
try:
response = text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_language_kwarg_spanish(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ async def test_bad_model_version_error(self, resource_group, location, text_anal
try:
result = await text_analytics.analyze_sentiment(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand Down Expand Up @@ -503,8 +503,8 @@ async def test_missing_input_records_error(self, resource_group, location, text_
try:
result = await text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -516,8 +516,8 @@ async def test_duplicate_ids_error(self, resource_group, location, text_analytic
try:
result = await text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -529,8 +529,8 @@ async def test_batch_size_over_limit_error(self, resource_group, location, text_
try:
response = await text_analytics.analyze_sentiment(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ def test_bad_model_version_error(self, resource_group, location, text_analytics_
try:
result = text_analytics.detect_language(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_document_errors(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -476,8 +476,8 @@ def test_missing_input_records_error(self, resource_group, location, text_analyt
try:
result = text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_duplicate_ids_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -489,8 +489,8 @@ def test_duplicate_ids_error(self, resource_group, location, text_analytics_acco
try:
result = text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_batch_size_over_limit_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -501,8 +501,8 @@ def test_batch_size_over_limit_error(self, resource_group, location, text_analyt
try:
response = text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@pytest.mark.skip(reason="Service bug returns invalidDocument here. Unskip after v3.0-preview.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ async def test_bad_model_version_error(self, resource_group, location, text_anal
try:
result = await text_analytics.detect_language(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -519,8 +519,8 @@ async def test_missing_input_records_error(self, resource_group, location, text_
try:
result = await text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -532,8 +532,8 @@ async def test_duplicate_ids_error(self, resource_group, location, text_analytic
try:
result = await text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -545,8 +545,8 @@ async def test_batch_size_over_limit_error(self, resource_group, location, text_
try:
response = await text_analytics.detect_language(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ def test_bad_model_version_error(self, resource_group, location, text_analytics_
try:
result = text_analytics.extract_key_phrases(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_document_errors(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down Expand Up @@ -428,8 +428,8 @@ def test_missing_input_records_error(self, resource_group, location, text_analyt
try:
result = text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_duplicate_ids_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -440,8 +440,8 @@ def test_duplicate_ids_error(self, resource_group, location, text_analytics_acco
try:
result = text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_batch_size_over_limit_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -451,8 +451,8 @@ def test_batch_size_over_limit_error(self, resource_group, location, text_analyt
try:
response = text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_language_kwarg_spanish(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ async def test_bad_model_version_error(self, resource_group, location, text_anal
try:
result = await text_analytics.extract_key_phrases(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand Down Expand Up @@ -471,8 +471,8 @@ async def test_missing_input_records_error(self, resource_group, location, text_
try:
result = await text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -485,8 +485,8 @@ async def test_duplicate_ids_error(self, resource_group, location, text_analytic
try:
result = await text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand All @@ -498,8 +498,8 @@ async def test_batch_size_over_limit_error(self, resource_group, location, text_
try:
response = await text_analytics.extract_key_phrases(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
@AsyncTextAnalyticsTest.await_prepared_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ def test_bad_model_version_error(self, resource_group, location, text_analytics_
try:
result = text_analytics.recognize_entities(docs, model_version="bad")
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidRequest")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidRequest")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_document_errors(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down Expand Up @@ -443,8 +443,8 @@ def test_missing_input_records_error(self, resource_group, location, text_analyt
try:
result = text_analytics.recognize_entities(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "MissingInputRecords")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "MissingInputRecords")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_duplicate_ids_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -455,8 +455,8 @@ def test_duplicate_ids_error(self, resource_group, location, text_analytics_acco
try:
result = text_analytics.recognize_entities(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocument")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocument")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_batch_size_over_limit_error(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand All @@ -466,8 +466,8 @@ def test_batch_size_over_limit_error(self, resource_group, location, text_analyt
try:
response = text_analytics.recognize_entities(docs)
except HttpResponseError as err:
self.assertEqual(err.error_code, "InvalidDocumentBatch")
self.assertIsNotNone(err.message)
self.assertEqual(err.error.code, "InvalidDocumentBatch")
self.assertIsNotNone(err.error.message)

@GlobalTextAnalyticsAccountPreparer()
def test_language_kwarg_spanish(self, resource_group, location, text_analytics_account, text_analytics_account_key):
Expand Down
Loading