Skip to content

Commit

Permalink
Replace region mismatch with invalid S3 object
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval authored Jun 20, 2024
2 parents ef16cf2 + c1d12e0 commit f21bf96
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
5 changes: 5 additions & 0 deletions textractor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ class UnsupportedDocumentException(Exception):
"""Raised by the Textract API when the document could not be processed"""

pass

class InvalidS3ObjectException(Exception):
"""Raised by the Textract API when an S3 object could not be accessed"""

pass
38 changes: 19 additions & 19 deletions textractor/textractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
from textractor.utils.pdf_utils import rasterize_pdf
from textractor.exceptions import (
InputError,
RegionMismatchError,
IncorrectMethodException,
MissingDependencyException,
UnhandledCaseException,
UnsupportedDocumentException,
InvalidS3ObjectException,
)


Expand Down Expand Up @@ -226,12 +226,12 @@ def detect_document_text(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
elif exception.__class__.__name__ == "UnsupportedDocumentException":
raise UnsupportedDocumentException(
"Textract returned an UnsupportedDocumentException, if file_source is a PDF, make sure that it only has one page or use start_document_text_detection. If your file_source is an image, make sure that it is not larger than 5MB."
"Textract returned UnsupportedDocumentException, if file_source is a PDF, make sure that it only has one page or use start_document_text_detection. If your file_source is an image, make sure that it is not larger than 5MB."
)
raise exception

Expand Down Expand Up @@ -317,8 +317,8 @@ def start_document_text_detection(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
raise exception

Expand Down Expand Up @@ -439,8 +439,8 @@ def analyze_document(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
elif exception.__class__.__name__ == "UnsupportedDocumentException":
raise UnsupportedDocumentException(
Expand Down Expand Up @@ -560,8 +560,8 @@ def start_document_analysis(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
raise exception

Expand Down Expand Up @@ -600,7 +600,7 @@ def analyze_id(
:param save_image: Saves the images in the returned Document object for visualizing the results, defaults to False
:type save_image: bool, optional
:raises InputError: Raised when the file_source could not be parsed
:raises RegionMismatchError: Raised when the S3 object passed as file source is in a region that does not match the one used to create the Textractor object.
:raises InvalidS3ObjectException: Raised when the S3 object passed as file source is in a region that does not match the one used to create the Textractor object.
:raises exception: Raised when the Textract call fails
:return: Document
:rtype: Document
Expand Down Expand Up @@ -628,8 +628,8 @@ def analyze_id(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
raise exception

Expand All @@ -654,7 +654,7 @@ def analyze_expense(
:type save_image: bool, optional
:raises IncorrectMethodException: Raised when the file source type is incompatible with the Textract API being called
:raises InputError: Raised when the file source type is invalid
:raises RegionMismatchError: Raised when the file source region is different the API region.
:raises InvalidS3ObjectException: Raised when the file source region is different the API region.
:raises exception: Raised if the Textract API call fails
:return: Document
:rtype: Document
Expand Down Expand Up @@ -704,8 +704,8 @@ def analyze_expense(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
raise exception

Expand Down Expand Up @@ -745,7 +745,7 @@ def start_expense_analysis(
and necessary only if the customer wants to visualize bounding boxes for their document entities.
:type save_image: bool
:raises InputError: Raised when the file source type is invalid
:raises RegionMismatchError: Raised when the file source region is different the API region.
:raises InvalidS3ObjectException: Raised when the file source region is different the API region.
:raises exception: Raised if the Textract API call fails
:return: Lazy-loaded Document object
:rtype: LazyDocument
Expand Down Expand Up @@ -790,8 +790,8 @@ def start_expense_analysis(
)
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise RegionMismatchError(
"Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same."
raise InvalidS3ObjectException(
"Textract returned InvalidS3ObjectException. Ensure that the s3 path is correct and that both the Textract API and the bucket are in the same region."
)
raise exception

Expand Down

0 comments on commit f21bf96

Please sign in to comment.