Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
docs(samples): Added extra exception handling to operation samples (#393
Browse files Browse the repository at this point in the history
)
  • Loading branch information
holtskinner authored Nov 2, 2022
1 parent 1f131c8 commit fa0f715
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import re

from google.api_core.client_options import ClientOptions
from google.api_core.exceptions import RetryError
from google.cloud import documentai, storage

# TODO(developer): Uncomment these variables before running the sample.
Expand All @@ -39,7 +40,7 @@ def batch_process_documents_processor_version(
input_mime_type: str,
gcs_output_bucket: str,
gcs_output_uri_prefix: str,
timeout: int = 300,
timeout: int = 400,
):

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

# NOTE: Can also use callbacks for asynchronous processing
#
Expand Down
11 changes: 8 additions & 3 deletions samples/snippets/batch_process_documents_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import re

from google.api_core.client_options import ClientOptions
from google.api_core.exceptions import RetryError
from google.cloud import documentai, storage

# TODO(developer): Uncomment these variables before running the sample.
Expand All @@ -37,7 +38,7 @@ def batch_process_documents(
input_mime_type: str,
gcs_output_bucket: str,
gcs_output_uri_prefix: str,
timeout: int = 300,
timeout: int = 400,
):

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

# NOTE: Can also use callbacks for asynchronous processing
#
Expand Down
1 change: 0 additions & 1 deletion samples/snippets/cancel_operation_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ def test_cancel_operation(capsys):
out, _ = capsys.readouterr()

assert "Operation" in out
assert "cancelled" in out
11 changes: 7 additions & 4 deletions samples/snippets/get_operation_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# [START documentai_get_operation]

from google.api_core.client_options import ClientOptions
from google.api_core.exceptions import NotFound
from google.cloud import documentai
from google.longrunning.operations_pb2 import GetOperationRequest

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

# Make GetOperation request
operation = client.get_operation(request=request)

# Print the Operation Information
print(operation)
try:
operation = client.get_operation(request=request)
# Print the Operation Information
print(operation)
except (NotFound) as e:
print(e.message)


# [END documentai_get_operation]
1 change: 0 additions & 1 deletion samples/snippets/list_operations_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ def test_list_operations(capsys):
out, _ = capsys.readouterr()

assert "operations" in out
assert "BatchProcessMetadata" in out
7 changes: 6 additions & 1 deletion samples/snippets/poll_operation_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from time import sleep

from google.api_core.client_options import ClientOptions
from google.api_core.exceptions import NotFound
from google.cloud import documentai
from google.longrunning.operations_pb2 import GetOperationRequest

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

while True:
# Make GetOperation request
operation = client.get_operation(request=request)
try:
operation = client.get_operation(request=request)
except (NotFound) as e:
print(e.message)
break

# Print the Operation Information
print(operation)
Expand Down

1 comment on commit fa0f715

@yan-hic
Copy link

Choose a reason for hiding this comment

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

For what's worth, operation raises also an error on GoogleAPICallError when an individual document could not be processed for an unknown reason. In our case, we got error code: 13 Internal error encountered.

Please sign in to comment.