Skip to content

Commit b74e81d

Browse files
authored
fix: Incorrect 500 response for invalid requests (#69)
1 parent 18d1ca2 commit b74e81d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/server.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ def get_db():
6969
db = SessionLocal()
7070
try:
7171
yield db
72-
except Exception as e:
73-
print(f"An error occurred while accessing the database: {e}")
7472
finally:
7573
db.close()
7674

@@ -93,13 +91,17 @@ def get_term_number(db, year: int, term: str) -> int:
9391
courses = db.query(Course).filter(Course.year == year).all()
9492

9593
if not courses:
96-
raise Exception(f"No courses found for year: {year}")
94+
raise HTTPException(
95+
status_code=404, detail=f"No courses found for year: {year}"
96+
)
9797

9898
for course in courses:
9999
if course.term_descr == term:
100100
return course.term
101101

102-
raise Exception(f"Invalid term: {term} for year: {year}")
102+
raise HTTPException(
103+
status_code=404, detail=f"Invalid term: {term} for year: {year}"
104+
)
103105

104106

105107
def meeting_date_convert(raw_date: str) -> dict[str]:
@@ -342,11 +344,11 @@ def get_course(course_cid: str, db: Session = Depends(get_db)):
342344
"""
343345
course = db.query(Course).filter(Course.id == course_cid).first()
344346

345-
course_id = course.course_id
346-
347347
if not course:
348348
raise HTTPException(status_code=404, detail="Course not found")
349349

350+
course_id = course.course_id
351+
350352
course_details = (
351353
db.query(CourseDetail).filter(CourseDetail.course_id == course_id).first()
352354
)

0 commit comments

Comments
 (0)