Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DH-4806 Raise 400 and 404 error for GET endpoints #199

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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