refactor(api): Refactor error handling in add() and consume() functions
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
80050f1874
commit
f7e90567d7
7
app.py
7
app.py
@ -193,12 +193,17 @@ def add():
|
|||||||
spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key)
|
spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key)
|
||||||
|
|
||||||
good = spider.get_good(barcode)
|
good = spider.get_good(barcode)
|
||||||
|
try:
|
||||||
if add_product(good, location):
|
if add_product(good, location):
|
||||||
response_data = {"message": "New item added successfully"}
|
response_data = {"message": "New item added successfully"}
|
||||||
return jsonify(response_data), 200
|
return jsonify(response_data), 200
|
||||||
else:
|
else:
|
||||||
response_data = {"message": "Fail to add new item"}
|
response_data = {"message": "Fail to add new item"}
|
||||||
return jsonify(response_data), 400
|
return jsonify(response_data), 400
|
||||||
|
except Exception as e:
|
||||||
|
error_message = str(e)
|
||||||
|
response_data = {"message": error_message}
|
||||||
|
return jsonify(response_data), 400
|
||||||
|
|
||||||
|
|
||||||
@app.route("/consume", methods=["POST"])
|
@app.route("/consume", methods=["POST"])
|
||||||
@ -211,7 +216,7 @@ def consume():
|
|||||||
return jsonify(response_data), 200
|
return jsonify(response_data), 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_message = str(e)
|
error_message = str(e)
|
||||||
response_data = {"error": error_message}
|
response_data = {"message": error_message}
|
||||||
return jsonify(response_data), 400
|
return jsonify(response_data), 400
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user