Skip to content

Handle inventory errors and improve checkout process #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
42 changes: 29 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os
from flask import Flask, request, json, abort
from flask import Flask, request, json, abort, jsonify, make_response
from flask_cors import CORS

import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

class InventoryError(Exception):
def __init__(self, message, item_id=None):
super().__init__(message)
self.item_id = item_id

sentry_sdk.init(
dsn="https://[email protected]/1316515",
integrations=[FlaskIntegration()],
Expand Down Expand Up @@ -37,13 +42,19 @@

def process_order(cart):
global Inventory
tempInventory = Inventory
tempInventory = Inventory.copy()
for item in cart:
if Inventory[item['id']] <= 0:
raise Exception("Not enough inventory for " + item['id'])
item_id = item['id']
quantity = item.get('quantity', 1)

if tempInventory[item_id] < quantity:
raise InventoryError(
f"Not enough inventory for '{item_id}'. Requested: {quantity}, Available: {tempInventory[item_id]}",
item_id
)
else:
tempInventory[item['id']] -= 1
print 'Success: ' + item['id'] + ' was purchased, remaining stock is ' + str(tempInventory[item['id']])
tempInventory[item_id] -= quantity
print('Success: ' + item_id + ' was purchased, remaining stock is ' + str(tempInventory[item_id]))
Inventory = tempInventory

@app.before_request
Expand All @@ -65,11 +76,16 @@

@app.route('/checkout', methods=['POST'])
def checkout():
try:
order = json.loads(request.data)
print("Processing order for: " + order["email"])
cart = order["cart"]

process_order(cart)

order = json.loads(request.data)
print "Processing order for: " + order["email"]
cart = order["cart"]

process_order(cart)

return 'Success'
return jsonify(message="Order processed successfully"), 200
except InventoryError as ie:
return make_response(jsonify(error=str(ie), itemId=ie.item_id), 400)

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix

AI 18 days ago

To fix the issue, the error message returned to the user should be generic and avoid exposing internal application state. The detailed error message from the InventoryError exception can be logged for debugging purposes, but the response to the user should only indicate that there was an issue with their request.

The best way to implement this fix is to replace the str(ie) usage with a generic error message, such as "Insufficient inventory for the requested item." Additionally, the detailed error message can be logged using sentry_sdk.capture_exception(ie) or another logging mechanism.

Suggested changeset 1
app.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -87,3 +87,4 @@
     except InventoryError as ie:
-        return make_response(jsonify(error=str(ie), itemId=ie.item_id), 400)
+        sentry_sdk.capture_exception(ie)
+        return make_response(jsonify(error="Insufficient inventory for the requested item.", itemId=ie.item_id), 400)
     except Exception as e:
EOF
@@ -87,3 +87,4 @@
except InventoryError as ie:
return make_response(jsonify(error=str(ie), itemId=ie.item_id), 400)
sentry_sdk.capture_exception(ie)
return make_response(jsonify(error="Insufficient inventory for the requested item.", itemId=ie.item_id), 400)
except Exception as e:
Copilot is powered by AI and may make mistakes. Always verify output.
except Exception as e:
sentry_sdk.capture_exception(e)
return make_response(jsonify(error="An unexpected server error occurred..."), 500)
Loading