Skip to content

Commit

Permalink
DH-4806 Raise 400 and 404 error for GET endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 committed Oct 9, 2023
1 parent f074cff commit 0e9c7d0
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions dataherald/api/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from bson import json_util
from bson.objectid import ObjectId
from bson.objectid import InvalidId, ObjectId
from fastapi import BackgroundTasks, HTTPException
from overrides import override

Expand Down Expand Up @@ -272,7 +272,15 @@ def list_table_descriptions(
@override
def get_table_description(self, table_description_id: str) -> TableDescription:
scanner_repository = TableDescriptionRepository(self.storage)
return scanner_repository.find_by_id(table_description_id)

try:
result = scanner_repository.find_by_id(table_description_id)
except InvalidId as e:
raise HTTPException(status_code=400, detail=str(e)) from e

if not result:
raise HTTPException(status_code=404, detail="Table description not found")
return result

@override
def get_responses(self, question_id: str | None = None) -> list[Response]:
Expand All @@ -285,20 +293,39 @@ def get_responses(self, question_id: str | None = None) -> list[Response]:
@override
def get_response(self, response_id: str) -> Response:
response_repository = ResponseRepository(self.storage)
return response_repository.find_by_id(response_id)

try:
result = response_repository.find_by_id(response_id)
except InvalidId as e:
raise HTTPException(status_code=400, detail=str(e)) from e

if not result:
raise HTTPException(status_code=404, detail="Question not found")

return result

@override
def get_questions(self, db_connection_id: str | None = None) -> list[Question]:
question_repository = QuestionRepository(self.storage)
query = {}
if db_connection_id:
query = {"db_connection_id": ObjectId(db_connection_id)}

return question_repository.find_by(query)

@override
def get_question(self, question_id: str) -> Question:
question_repository = QuestionRepository(self.storage)
return question_repository.find_by_id(question_id)

try:
result = question_repository.find_by_id(question_id)
except InvalidId as e:
raise HTTPException(status_code=400, detail=str(e)) from e

if not result:
raise HTTPException(status_code=404, detail="Question not found")

return result

@override
def add_golden_records(
Expand Down

0 comments on commit 0e9c7d0

Please sign in to comment.