Skip to content

Commit 80dece9

Browse files
committed
flask now does error handling [flask_error_handling]
1 parent f20c479 commit 80dece9

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

flask_app.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
app = Flask(__name__)
1414

1515

16+
def is_valid_sku(sku, batches):
17+
return sku in {b.sku for b in batches}
18+
19+
1620
@app.route("/allocate", methods=["POST"])
1721
def allocate_endpoint():
1822
session = get_session()
@@ -21,7 +25,13 @@ def allocate_endpoint():
2125
request.json["orderid"], request.json["sku"], request.json["qty"],
2226
)
2327

24-
batchref = model.allocate(line, batches)
28+
if not is_valid_sku(line.sku, batches):
29+
return {"message": f"Invalid sku {line.sku}"}, 400
30+
31+
try:
32+
batchref = model.allocate(line, batches)
33+
except model.OutOfStock as e:
34+
return {"message": str(e)}, 400
2535

2636
session.commit()
2737
return {"batchref": batchref}, 201

0 commit comments

Comments
 (0)