Pricing: clear message for non-numeric quantity (fixes #30); manual Chapter 12
This commit is contained in:
+6
-3
@@ -7,9 +7,12 @@ UNIT_PRICE = 12
|
||||
COMMIT = os.environ.get("APP_COMMIT", "development")
|
||||
|
||||
|
||||
QUANTITY_ERROR = "quantity must be a whole number between 1 and 1000"
|
||||
|
||||
|
||||
def total_price(quantity):
|
||||
if quantity < 1 or quantity > 1000:
|
||||
raise ValueError("quantity must be between 1 and 1000")
|
||||
raise ValueError(QUANTITY_ERROR)
|
||||
return UNIT_PRICE * quantity
|
||||
|
||||
|
||||
@@ -28,6 +31,6 @@ def price():
|
||||
try:
|
||||
quantity = int(request.args.get("quantity", "1"))
|
||||
total = total_price(quantity)
|
||||
except ValueError as error:
|
||||
return jsonify(error=str(error)), 400
|
||||
except ValueError:
|
||||
return jsonify(error=QUANTITY_ERROR), 400
|
||||
return jsonify(quantity=quantity, total=total, currency="USD")
|
||||
|
||||
Reference in New Issue
Block a user