Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import json
from models import Product, Category, User, db, Comment, product_categories
import boto3
from botocore.exceptions import NoCredentialsError
from botocore.exceptions import NoCredentialsError, ClientError
from vulnerable_image_processor import process_image
from sklearn.metrics.pairwise import cosine_similarity
import pickle
Expand Down Expand Up @@ -236,8 +236,23 @@ def product_lookup():

features_file_key = 'image_features.pkl' # S3 key for the features file

response = s3.get_object(Bucket=bucket_name, Key=features_file_key)
features = pickle.loads(response['Body'].read())
try:
response = s3.get_object(Bucket=bucket_name, Key=features_file_key)
features = pickle.loads(response['Body'].read())
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchKey':
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
Comment on lines +244 to +247

Copilot AI Mar 3, 2026

Copy link

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.
)
return jsonify({
'error': (
'Image features file not found. Please run the image preprocessing '
'pipeline in the SageMaker notebook first. See README for setup instructions.'
)
}), 503
raise

Copilot AI Mar 3, 2026

Copy link

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.

payload = {
'bucket_name': bucket_name,
Expand Down
Loading