From f7e90567d72dfb0e47aac5c50cd966cd5401c46f Mon Sep 17 00:00:00 2001 From: Ching Date: Sun, 3 Mar 2024 20:16:24 +0800 Subject: [PATCH] refactor(api): Refactor error handling in add() and consume() functions --- app.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 069e0ad..1a3e0cc 100644 --- a/app.py +++ b/app.py @@ -193,11 +193,16 @@ def add(): spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key) good = spider.get_good(barcode) - if add_product(good, location): - response_data = {"message": "New item added successfully"} - return jsonify(response_data), 200 - else: - response_data = {"message": "Fail to add new item"} + try: + if add_product(good, location): + response_data = {"message": "New item added successfully"} + return jsonify(response_data), 200 + else: + response_data = {"message": "Fail to add new item"} + return jsonify(response_data), 400 + except Exception as e: + error_message = str(e) + response_data = {"message": error_message} return jsonify(response_data), 400 @@ -211,7 +216,7 @@ def consume(): return jsonify(response_data), 200 except Exception as e: error_message = str(e) - response_data = {"error": error_message} + response_data = {"message": error_message} return jsonify(response_data), 400