Skip to content

Commit b550bdf

Browse files
[formrecognizer] Update error handling in samples (Azure#29058)
* update error handling in samples * raise errors and check for more cases * update async invoice samples
1 parent 781de0f commit b550bdf

38 files changed

+380
-380
lines changed

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_business_cards_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,19 @@ async def main():
165165
try:
166166
asyncio.run(main())
167167
except HttpResponseError as error:
168+
print("For more information about troubleshooting errors, see the following guide: "
169+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
168170
# Examples of how to check an HttpResponseError
169171
# Check by error code:
170172
if error.error is not None:
171-
if error.error.code == "InvalidRequest":
172-
print(f"Received an invalid request error: {error.error}")
173-
sys.exit(1)
174173
if error.error.code == "InvalidImage":
175174
print(f"Received an invalid image error: {error.error}")
176-
sys.exit(1)
175+
if error.error.code == "InvalidRequest":
176+
print(f"Received an invalid request error: {error.error}")
177+
# Raise the error again after printing it
178+
raise
177179
# If the inner error is None and then it is possible to check the message to get more information:
178-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
179-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
180-
print(f"Uh-oh! Something unexpected happened: {error}")
181-
sys.exit(1)
182-
# Print the full error content:
183-
print(f"Full HttpResponseError: {error}")
180+
if "Invalid request".casefold() in error.message.casefold():
181+
print(f"Uh-oh! Seems there was an invalid request: {error}")
182+
# Raise the error again
183+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_custom_documents_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,19 @@ async def main():
142142
try:
143143
asyncio.run(main())
144144
except HttpResponseError as error:
145+
print("For more information about troubleshooting errors, see the following guide: "
146+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
145147
# Examples of how to check an HttpResponseError
146148
# Check by error code:
147149
if error.error is not None:
148-
if error.error.code == "InvalidRequest":
149-
print(f"Received an invalid request error: {error.error}")
150-
sys.exit(1)
151150
if error.error.code == "InvalidImage":
152151
print(f"Received an invalid image error: {error.error}")
153-
sys.exit(1)
152+
if error.error.code == "InvalidRequest":
153+
print(f"Received an invalid request error: {error.error}")
154+
# Raise the error again after printing it
155+
raise
154156
# If the inner error is None and then it is possible to check the message to get more information:
155-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
156-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
157-
print(f"Uh-oh! Something unexpected happened: {error}")
158-
sys.exit(1)
159-
# Print the full error content:
160-
print(f"Full HttpResponseError: {error}")
157+
if "Invalid request".casefold() in error.message.casefold():
158+
print(f"Uh-oh! Seems there was an invalid request: {error}")
159+
# Raise the error again
160+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_general_documents_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,19 @@ async def main():
161161
try:
162162
asyncio.run(main())
163163
except HttpResponseError as error:
164+
print("For more information about troubleshooting errors, see the following guide: "
165+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
164166
# Examples of how to check an HttpResponseError
165167
# Check by error code:
166168
if error.error is not None:
167-
if error.error.code == "InvalidRequest":
168-
print(f"Received an invalid request error: {error.error}")
169-
sys.exit(1)
170169
if error.error.code == "InvalidImage":
171170
print(f"Received an invalid image error: {error.error}")
172-
sys.exit(1)
171+
if error.error.code == "InvalidRequest":
172+
print(f"Received an invalid request error: {error.error}")
173+
# Raise the error again after printing it
174+
raise
173175
# If the inner error is None and then it is possible to check the message to get more information:
174-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
175-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
176-
print(f"Uh-oh! Something unexpected happened: {error}")
177-
sys.exit(1)
178-
# Print the full error content:
179-
print(f"Full HttpResponseError: {error}")
176+
if "Invalid request".casefold() in error.message.casefold():
177+
print(f"Uh-oh! Seems there was an invalid request: {error}")
178+
# Raise the error again
179+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_identity_documents_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ async def main():
123123
try:
124124
asyncio.run(main())
125125
except HttpResponseError as error:
126+
print("For more information about troubleshooting errors, see the following guide: "
127+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
126128
# Examples of how to check an HttpResponseError
127129
# Check by error code:
128130
if error.error is not None:
129-
if error.error.code == "InvalidRequest":
130-
print(f"Received an invalid request error: {error.error}")
131-
sys.exit(1)
132131
if error.error.code == "InvalidImage":
133132
print(f"Received an invalid image error: {error.error}")
134-
sys.exit(1)
133+
if error.error.code == "InvalidRequest":
134+
print(f"Received an invalid request error: {error.error}")
135+
# Raise the error again after printing it
136+
raise
135137
# If the inner error is None and then it is possible to check the message to get more information:
136-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
137-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
138-
print(f"Uh-oh! Something unexpected happened: {error}")
139-
sys.exit(1)
140-
# Print the full error content:
141-
print(f"Full HttpResponseError: {error}")
138+
if "Invalid request".casefold() in error.message.casefold():
139+
print(f"Uh-oh! Seems there was an invalid request: {error}")
140+
# Raise the error again
141+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_invoices_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,19 @@ async def main():
316316
try:
317317
asyncio.run(main())
318318
except HttpResponseError as error:
319+
print("For more information about troubleshooting errors, see the following guide: "
320+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
319321
# Examples of how to check an HttpResponseError
320322
# Check by error code:
321323
if error.error is not None:
322-
if error.error.code == "InvalidRequest":
323-
print(f"Received an invalid request error: {error.error}")
324-
sys.exit(1)
325324
if error.error.code == "InvalidImage":
326325
print(f"Received an invalid image error: {error.error}")
327-
sys.exit(1)
326+
if error.error.code == "InvalidRequest":
327+
print(f"Received an invalid request error: {error.error}")
328+
# Raise the error again after printing it
329+
raise
328330
# If the inner error is None and then it is possible to check the message to get more information:
329-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
330-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
331-
print(f"Uh-oh! Something unexpected happened: {error}")
332-
sys.exit(1)
333-
# Print the full error content:
334-
print(f"Full HttpResponseError: {error}")
331+
if "Invalid request".casefold() in error.message.casefold():
332+
print(f"Uh-oh! Seems there was an invalid request: {error}")
333+
# Raise the error again
334+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_layout_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ async def main():
147147
try:
148148
asyncio.run(main())
149149
except HttpResponseError as error:
150+
print("For more information about troubleshooting errors, see the following guide: "
151+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
150152
# Examples of how to check an HttpResponseError
151153
# Check by error code:
152154
if error.error is not None:
153-
if error.error.code == "InvalidRequest":
154-
print(f"Received an invalid request error: {error.error}")
155-
sys.exit(1)
156155
if error.error.code == "InvalidImage":
157156
print(f"Received an invalid image error: {error.error}")
158-
sys.exit(1)
157+
if error.error.code == "InvalidRequest":
158+
print(f"Received an invalid request error: {error.error}")
159+
# Raise the error again after printing it
160+
raise
159161
# If the inner error is None and then it is possible to check the message to get more information:
160-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
161-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
162-
print(f"Uh-oh! Something unexpected happened: {error}")
163-
sys.exit(1)
164-
# Print the full error content:
165-
print(f"Full HttpResponseError: {error}")
162+
if "Invalid request".casefold() in error.message.casefold():
163+
print(f"Uh-oh! Seems there was an invalid request: {error}")
164+
# Raise the error again
165+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_read_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ async def main():
119119
try:
120120
asyncio.run(main())
121121
except HttpResponseError as error:
122+
print("For more information about troubleshooting errors, see the following guide: "
123+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
122124
# Examples of how to check an HttpResponseError
123125
# Check by error code:
124126
if error.error is not None:
125-
if error.error.code == "InvalidRequest":
126-
print(f"Received an invalid request error: {error.error}")
127-
sys.exit(1)
128127
if error.error.code == "InvalidImage":
129128
print(f"Received an invalid image error: {error.error}")
130-
sys.exit(1)
129+
if error.error.code == "InvalidRequest":
130+
print(f"Received an invalid request error: {error.error}")
131+
# Raise the error again after printing it
132+
raise
131133
# If the inner error is None and then it is possible to check the message to get more information:
132-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
133-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
134-
print(f"Uh-oh! Something unexpected happened: {error}")
135-
sys.exit(1)
136-
# Print the full error content:
137-
print(f"Full HttpResponseError: {error}")
134+
if "Invalid request".casefold() in error.message.casefold():
135+
print(f"Uh-oh! Seems there was an invalid request: {error}")
136+
# Raise the error again
137+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_receipts_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,19 @@ async def main():
133133
try:
134134
asyncio.run(main())
135135
except HttpResponseError as error:
136+
print("For more information about troubleshooting errors, see the following guide: "
137+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
136138
# Examples of how to check an HttpResponseError
137139
# Check by error code:
138140
if error.error is not None:
139-
if error.error.code == "InvalidRequest":
140-
print(f"Received an invalid request error: {error.error}")
141-
sys.exit(1)
142141
if error.error.code == "InvalidImage":
143142
print(f"Received an invalid image error: {error.error}")
144-
sys.exit(1)
143+
if error.error.code == "InvalidRequest":
144+
print(f"Received an invalid request error: {error.error}")
145+
# Raise the error again after printing it
146+
raise
145147
# If the inner error is None and then it is possible to check the message to get more information:
146-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
147-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
148-
print(f"Uh-oh! Something unexpected happened: {error}")
149-
sys.exit(1)
150-
# Print the full error content:
151-
print(f"Full HttpResponseError: {error}")
148+
if "Invalid request".casefold() in error.message.casefold():
149+
print(f"Uh-oh! Seems there was an invalid request: {error}")
150+
# Raise the error again
151+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_receipts_from_url_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@ async def main():
125125
try:
126126
asyncio.run(main())
127127
except HttpResponseError as error:
128+
print("For more information about troubleshooting errors, see the following guide: "
129+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
128130
# Examples of how to check an HttpResponseError
129131
# Check by error code:
130132
if error.error is not None:
131-
if error.error.code == "InvalidRequest":
132-
print(f"Received an invalid request error: {error.error}")
133-
sys.exit(1)
134133
if error.error.code == "InvalidImage":
135134
print(f"Received an invalid image error: {error.error}")
136-
sys.exit(1)
135+
if error.error.code == "InvalidRequest":
136+
print(f"Received an invalid request error: {error.error}")
137+
# Raise the error again after printing it
138+
raise
137139
# If the inner error is None and then it is possible to check the message to get more information:
138-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
139-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
140-
print(f"Uh-oh! Something unexpected happened: {error}")
141-
sys.exit(1)
142-
# Print the full error content:
143-
print(f"Full HttpResponseError: {error}")
140+
if "Invalid request".casefold() in error.message.casefold():
141+
print(f"Uh-oh! Seems there was an invalid request: {error}")
142+
# Raise the error again
143+
raise

sdk/formrecognizer/azure-ai-formrecognizer/samples/v3.2/async_samples/sample_analyze_tax_us_w2_async.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,19 @@ async def main():
339339
try:
340340
asyncio.run(main())
341341
except HttpResponseError as error:
342+
print("For more information about troubleshooting errors, see the following guide: "
343+
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
342344
# Examples of how to check an HttpResponseError
343345
# Check by error code:
344346
if error.error is not None:
345-
if error.error.code == "InvalidRequest":
346-
print(f"Received an invalid request error: {error.error}")
347-
sys.exit(1)
348347
if error.error.code == "InvalidImage":
349348
print(f"Received an invalid image error: {error.error}")
350-
sys.exit(1)
349+
if error.error.code == "InvalidRequest":
350+
print(f"Received an invalid request error: {error.error}")
351+
# Raise the error again after printing it
352+
raise
351353
# If the inner error is None and then it is possible to check the message to get more information:
352-
filter_msg = ["Generic error", "Timeout", "Invalid request", "InvalidImage"]
353-
if any(example_error.casefold() in error.message.casefold() for example_error in filter_msg):
354-
print(f"Uh-oh! Something unexpected happened: {error}")
355-
sys.exit(1)
356-
# Print the full error content:
357-
print(f"Full HttpResponseError: {error}")
354+
if "Invalid request".casefold() in error.message.casefold():
355+
print(f"Uh-oh! Seems there was an invalid request: {error}")
356+
# Raise the error again
357+
raise

0 commit comments

Comments
 (0)