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

Improve error handling #27

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
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
Loading