Skip to content

Commit 34c0e3f

Browse files
committed
docs(samples): Added extra exception handling to operation samples (#393)
1 parent 6dc4b94 commit 34c0e3f

6 files changed

+29
-13
lines changed

batch_process_documents_processor_version_sample.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818

1919
from google.api_core.client_options import ClientOptions
20+
from google.api_core.exceptions import RetryError
2021
from google.cloud import documentai, storage
2122

2223
# TODO(developer): Uncomment these variables before running the sample.
@@ -39,7 +40,7 @@ def batch_process_documents_processor_version(
3940
input_mime_type: str,
4041
gcs_output_bucket: str,
4142
gcs_output_uri_prefix: str,
42-
timeout: int = 300,
43+
timeout: int = 400,
4344
):
4445

4546
# You must set the api_endpoint if you use a location other than 'us', e.g.:
@@ -90,8 +91,12 @@ def batch_process_documents_processor_version(
9091
# Continually polls the operation until it is complete.
9192
# This could take some time for larger files
9293
# Format: projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID
93-
print(f"Waiting for operation {operation.operation.name} to complete...")
94-
operation.result(timeout=timeout)
94+
try:
95+
print(f"Waiting for operation {operation.operation.name} to complete...")
96+
operation.result(timeout=timeout)
97+
# Catch exception when operation doesn't finish before timeout
98+
except (RetryError) as e:
99+
print(e.message)
95100

96101
# NOTE: Can also use callbacks for asynchronous processing
97102
#

batch_process_documents_sample.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818

1919
from google.api_core.client_options import ClientOptions
20+
from google.api_core.exceptions import RetryError
2021
from google.cloud import documentai, storage
2122

2223
# TODO(developer): Uncomment these variables before running the sample.
@@ -37,7 +38,7 @@ def batch_process_documents(
3738
input_mime_type: str,
3839
gcs_output_bucket: str,
3940
gcs_output_uri_prefix: str,
40-
timeout: int = 300,
41+
timeout: int = 400,
4142
):
4243

4344
# You must set the api_endpoint if you use a location other than 'us', e.g.:
@@ -86,8 +87,12 @@ def batch_process_documents(
8687
# Continually polls the operation until it is complete.
8788
# This could take some time for larger files
8889
# Format: projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID
89-
print(f"Waiting for operation {operation.operation.name} to complete...")
90-
operation.result(timeout=timeout)
90+
try:
91+
print(f"Waiting for operation {operation.operation.name} to complete...")
92+
operation.result(timeout=timeout)
93+
# Catch exception when operation doesn't finish before timeout
94+
except (RetryError) as e:
95+
print(e.message)
9196

9297
# NOTE: Can also use callbacks for asynchronous processing
9398
#

cancel_operation_sample_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ def test_cancel_operation(capsys):
3030
out, _ = capsys.readouterr()
3131

3232
assert "Operation" in out
33-
assert "cancelled" in out

get_operation_sample.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# [START documentai_get_operation]
1717

1818
from google.api_core.client_options import ClientOptions
19+
from google.api_core.exceptions import NotFound
1920
from google.cloud import documentai
2021
from google.longrunning.operations_pb2 import GetOperationRequest
2122

@@ -33,10 +34,12 @@ def get_operation_sample(location: str, operation_name: str):
3334
request = GetOperationRequest(name=operation_name)
3435

3536
# Make GetOperation request
36-
operation = client.get_operation(request=request)
37-
38-
# Print the Operation Information
39-
print(operation)
37+
try:
38+
operation = client.get_operation(request=request)
39+
# Print the Operation Information
40+
print(operation)
41+
except (NotFound) as e:
42+
print(e.message)
4043

4144

4245
# [END documentai_get_operation]

list_operations_sample_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ def test_list_operations(capsys):
2929
out, _ = capsys.readouterr()
3030

3131
assert "operations" in out
32-
assert "BatchProcessMetadata" in out

poll_operation_sample.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from time import sleep
1919

2020
from google.api_core.client_options import ClientOptions
21+
from google.api_core.exceptions import NotFound
2122
from google.cloud import documentai
2223
from google.longrunning.operations_pb2 import GetOperationRequest
2324

@@ -36,7 +37,11 @@ def poll_operation_sample(location: str, operation_name: str):
3637

3738
while True:
3839
# Make GetOperation request
39-
operation = client.get_operation(request=request)
40+
try:
41+
operation = client.get_operation(request=request)
42+
except (NotFound) as e:
43+
print(e.message)
44+
break
4045

4146
# Print the Operation Information
4247
print(operation)

0 commit comments

Comments
 (0)