Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Provide 404 for non-existent record
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Feb 19, 2024
1 parent 8877ca0 commit 032cc1f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/records/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
RosettaRecordsSearch,
RosettaRecordsSearchStats,
)
from fastapi import HTTPException

from .schemas import Record, RecordArchive, RecordCreator, RecordSearchResults

Expand Down Expand Up @@ -80,5 +81,8 @@ async def item(
id: str,
): # ) -> Record | RecordCreator | RecordArchive:
rosetta_api = RosettaRecordDetails()
result = rosetta_api.get_result(id)
try:
result = rosetta_api.get_result(id)
except Exception:
raise HTTPException(status_code=404, detail="Record not found")
return result
5 changes: 4 additions & 1 deletion app/sources/rosetta/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def get_result(self, id: str) -> dict:
return self.parse_results(raw_results, url)

def parse_results(self, raw_results, source_url):
parsed_data = RosettaResponseParser(raw_results)
try:
parsed_data = RosettaResponseParser(raw_results)
except Exception:
raise Exception("Respone is not recognised")
if (
parsed_data.type() == "record"
or parsed_data.type() == "aggregation"
Expand Down
6 changes: 5 additions & 1 deletion app/sources/rosetta/lib/response_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@

class RosettaResponseParser:
def __new__(cls, rosetta_data: dict, source_item: int = 0):
rosetta_data_source = rosetta_data["metadata"][source_item]["_source"]
rosetta_data_source = objects.get(
rosetta_data, f"metadata.{source_item}._source"
)
if not rosetta_data_source:
raise Exception("Invalid response structure")
return RosettaSourceParser(rosetta_data_source)


Expand Down

0 comments on commit 032cc1f

Please sign in to comment.