Skip to content

Commit

Permalink
Merge pull request #27 from ecmwf-projects/improve-error-handling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
aperezpredictia authored Aug 9, 2024
2 parents ef03c27 + c9d42c7 commit a23c08c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions cdsobs/api_rest/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def session_gen() -> Iterator[HttpAPISession]:
session.catalogue_session.close()


def make_http_exception(
status_code: int, message: str, traceback: str | None = None
) -> HTTPException:
detail = dict(message="Error: Observations API failed.")
if traceback is not None:
detail["traceback"] = traceback
http_exception = HTTPException(
status_code=status_code, detail=dict(message=message, traceback=traceback)
)
return http_exception


@router.post("/get_object_urls_and_check_size")
def get_object_urls_and_check_size(
retrieve_payload: RetrievePayload,
Expand All @@ -59,21 +71,23 @@ def get_object_urls_and_check_size(
catalogue_repository = CatalogueRepository(session.catalogue_session)
entries = _get_catalogue_entries(catalogue_repository, retrieve_args)
except DataNotFoundException as e:
raise HTTPException(status_code=500, detail=f"Error: {e}")
raise make_http_exception(status_code=500, message=f"Error: {e}")
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error: Observations API failed: {e}"
raise make_http_exception(
status_code=500,
message=f"Error: Observations API failed: {e}",
traceback=repr(e),
)
s3client = S3Client.from_config(session.cdsobs_config.s3config)
try:
object_urls = get_urls_and_check_size(
entries, retrieve_args, retrieve_payload.config.size_limit, s3client.base
)
except SizeError as e:
raise HTTPException(status_code=500, detail=f"Error: {e}")
raise HTTPException(status_code=500, detail=dict(message=f"Error: {e}"))
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error: Observations API failed: {e}"
raise make_http_exception(
status_code=500, message="Error: Observations API failed", traceback=repr(e)
)
return object_urls

Expand All @@ -100,8 +114,8 @@ def get_dataset_service_definition(dataset: str) -> ServiceDefinition:
try:
return get_service_definition(dataset)
except FileNotFoundError:
raise HTTPException(
status_code=404, detail=f"Service definition not found for {dataset=}"
raise make_http_exception(
status_code=404, message=f"Service definition not found for {dataset=}"
)


Expand Down
2 changes: 1 addition & 1 deletion tests/retrieve/test_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_adaptor_uscrn(tmp_path):
test_adaptor_config = {
"entry_point": "cads_adaptors:ObservationsAdaptor",
"collection_id": "insitu-observations-near-surface-temperature-us-climate-reference-network",
"obs_api_url": "http://obscatalogue.cads-obs.compute.cci2.ecmwf.int",
"obs_api_url": "http://localhost:8000",
"mapping": {
"remap": {
"time_aggregation": {
Expand Down

0 comments on commit a23c08c

Please sign in to comment.