aws-samples / aws-samples/amazon-textract-textractor

Misleading error message: InvalidS3ObjectException incorrectly attributed to region mismatch

Open
#443 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
493
Forks
163
PR merge metrics
No merged PRs in 30d

Description

**Description:**

The Textractor library catches `InvalidS3ObjectException` from AWS Textract and automatically assumes it's a region mismatch, but this exception can be raised for multiple reasons including IAM permissions issues.

**Current behavior:**
When AWS Textract raises `InvalidS3ObjectException`, Textractor catches it and raises:

```
RegionMismatchError: Region passed in the profile_name and S3 bucket do not match. Ensure the regions are the same.
```

**Problem:**
`InvalidS3ObjectException` is actually a generic error from AWS that means "Amazon Textract is unable to access the S3 object or the KMS key if specified in the request." This can be caused by:
1. IAM permission issues (missing s3:GetObject, textract:* permissions)
2. Missing Textract service role
3. KMS key access issues
4. Actual region mismatches (less common)

The library incorrectly assumes ALL `InvalidS3ObjectException` errors are region mismatches, which leads developers down the wrong troubleshooting path.

**Code location:**
This happens in multiple places in textractor.py (lines 229, 313, 442, 559, 627, 703, 789):
```python
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 exception
```

**Suggested fix:**
Either:
1. Pass through the original AWS error message instead of assuming it's a region issue:
```python
except Exception as exception:
if exception.__class__.__name__ == "InvalidS3ObjectException":
raise InvalidS3ObjectException(
f"Amazon Textract cannot access the S3 object. This could be due to IAM permissions, region mismatch, or KMS issues. Original error: {str(exception)}"
)
raise exception
````

2. Or simply let the original exception propagate without catching it, since the AWS error message is more informative.

**Reproduction:**
Use Textractor with IAM credentials that have S3 access but lack Textract permissions. The error will claim it's a region mismatch even when all regions are correctly configured.

Contributor guide

Open the contributing guide

Research direction

Start in textractor.py at the exception handlers around lines 229, 313, 442, 559, 627, 703, and 789, then reproduce the behavior with credentials lacking Textract permissions. Check how InvalidS3ObjectException is transformed and verify that the resulting error preserves the AWS access details instead of always reporting a region mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.