Skip to content

Commit 58b4693

Browse files
Refactor opportunity retrieval error handling
1 parent 646b909 commit 58b4693

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

labconnect/main/opportunity_routes.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,16 @@ def get_single_opportunity(opportunity_id: int):
6565
def get_opportunity_via_json():
6666
"""GET /opportunity expects a JSON payload with {"id": <int>}."""
6767
data = request.get_json()
68-
if not data:
69-
abort(400)
70-
71-
if "id" not in data:
72-
abort(400)
68+
opp_id_raw = data.get("id") if data else None
7369

7470
try:
75-
opp_id = int(data["id"])
76-
except ValueError:
77-
abort(400)
71+
opp_id = int(opp_id_raw)
72+
except (ValueError, TypeError):
73+
abort(400, description="Invalid or missing 'id' in JSON payload.")
7874

7975
opp = db.session.get(Opportunities, opp_id)
8076
if not opp:
81-
abort(404)
77+
abort(404, description="Opportunity not found.")
8278

8379
return opportunity_to_dict(opp)
8480

0 commit comments

Comments
 (0)