Skip to content

fix: handle NoSuchKey when image_features.pkl is missing in S3#17

Open
NJX-njx wants to merge 1 commit intoorcasecurity-research:mainfrom
NJX-njx:fix/issue-14-no-such-key-handling
Open

fix: handle NoSuchKey when image_features.pkl is missing in S3#17
NJX-njx wants to merge 1 commit intoorcasecurity-research:mainfrom
NJX-njx:fix/issue-14-no-such-key-handling

Conversation

@NJX-njx
Copy link
Copy Markdown

@NJX-njx NJX-njx commented Mar 3, 2026

Summary

Fixes #14

When users upload a photo for analysis without having run the image preprocessing pipeline in the SageMaker notebook, \image_features.pkl\ does not exist in S3. The backend previously threw an unhandled \NoSuchKey\ exception, resulting in a 500 error with the full Werkzeug debug traceback (including sensitive debug info).

Changes

  • Add \ClientError\ exception handling for \s3.get_object()\ in \product_lookup\
  • Return a user-friendly 503 response with clear instructions when \image_features.pkl\ is missing
  • Log the error for debugging while avoiding traceback exposure to end users

Testing

No AWS deployment required - the change is a straightforward exception handler that returns a JSON error response instead of propagating the exception.

Made with Cursor

…rcasecurity-research#14)

- Add ClientError exception handling for s3.get_object() in product_lookup
- Return user-friendly 503 error when image_features.pkl does not exist
- Prevents 500 debug traceback exposure when image preprocessing not run

Made-with: Cursor
Copilot AI review requested due to automatic review settings March 3, 2026 14:09
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a production error path in product_lookup when the S3 artifact image_features.pkl hasn’t been generated/uploaded yet, preventing an unhandled S3 exception from surfacing as a 500 with a Werkzeug debugger page.

Changes:

  • Catch botocore.exceptions.ClientError around s3.get_object() for image_features.pkl.
  • When the S3 error code is NoSuchKey, return a user-friendly JSON response with HTTP 503 and log guidance to run the preprocessing pipeline.
  • Add the ClientError import needed for the new exception handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/app.py
'pipeline in the SageMaker notebook first. See README for setup instructions.'
)
}), 503
raise
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The bare raise will propagate any non-NoSuchKey ClientError back to Flask; with debug=True in this app, that can still result in a Werkzeug debugger traceback being returned to end users. Consider returning a sanitized JSON error response for other S3 ClientError cases as well (and logging details server-side) instead of re-raising.

Suggested change
raise
app.logger.exception(
"Error retrieving features file '%s' from S3 bucket '%s'",
features_file_key,
bucket_name,
)
return jsonify({
'error': 'Unable to retrieve image features from storage. Please try again later.'
}), 500

Copilot uses AI. Check for mistakes.
Comment thread backend/app.py
Comment on lines +244 to +247
app.logger.error(
'image_features.pkl not found in S3 bucket %s. '
'Please run the image preprocessing pipeline in the SageMaker notebook first.',
bucket_name
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

This log line drops the actual S3 error details (code/message/request-id), which can make debugging harder. Consider including e.response['Error'] fields (or logging with exc_info=True) while still returning a user-friendly response to the client.

Suggested change
app.logger.error(
'image_features.pkl not found in S3 bucket %s. '
'Please run the image preprocessing pipeline in the SageMaker notebook first.',
bucket_name
error_info = e.response.get('Error', {})
request_id = e.response.get('ResponseMetadata', {}).get('RequestId')
app.logger.error(
'image_features.pkl not found in S3 bucket %s. '
'Please run the image preprocessing pipeline in the SageMaker notebook first. '
'S3 error code=%s, message=%s, request_id=%s',
bucket_name,
error_info.get('Code'),
error_info.get('Message'),
request_id,

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when analyzing photos (challenge 1)

2 participants