Skip to content

Commit 18477fe

Browse files
committed
flask now does error handling [flask_error_handling]
1 parent ee437e0 commit 18477fe

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎flask_app.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
get_session = sessionmaker(bind=create_engine(config.get_postgres_uri()))
1313
app = Flask(__name__)
1414

15+
def is_valid_sku(sku, batches):
16+
return sku in {b.sku for b in batches}
17+
1518
@app.route("/allocate", methods=['POST'])
1619
def allocate_endpoint():
1720
session = get_session()
@@ -22,7 +25,14 @@ def allocate_endpoint():
2225
request.json['qty'],
2326
)
2427

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

2736
session.commit()
2837
return jsonify({'batchref': batchref}), 201
38+

0 commit comments

Comments
 (0)