Skip to content

Commit

Permalink
fix: handling error properly
Browse files Browse the repository at this point in the history
fix: allow fragments to be queried
  • Loading branch information
xgui3783 committed Dec 17, 2024
1 parent b330147 commit 515468b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
9 changes: 6 additions & 3 deletions api/common/data_handlers/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def get_parcellation_labelled_map(parcellation_id: str, space_id: str, region_id
# raise e

@data_decorator(ROLE)
def get_resampled_map(parcellation_id: str, space_id: str):
def get_resampled_map(parcellation_id: str, space_id: str, name: str=""):
"""Retrieve and save a labelled map, resampled in space (if necessary), and then return the path of the map.
Args:
Expand All @@ -193,15 +193,18 @@ def get_resampled_map(parcellation_id: str, space_id: str):
path to statistical map, if a cached file is returned
"""
import os
full_filename = get_filename("resampled_map", parcellation_id, space_id, ext=".nii.gz")
full_filename = get_filename("resampled_map", parcellation_id, space_id, name, ext=".nii.gz")
if os.path.isfile(full_filename):
return full_filename, True

import siibra
import nibabel as nib
parcellation: siibra.core.parcellation.Parcellation = siibra.parcellations[parcellation_id]
parcellation_map = parcellation.get_map(siibra.spaces[space_id], siibra.MapType.LABELLED)
nii = parcellation_map.get_resampled_template()
if len(parcellation_map.fragments) > 0:
nii = parcellation_map.get_resampled_template(fragment=name)
else:
nii = parcellation_map.get_resampled_template()

assert isinstance(nii, nib.Nifti1Image), f"resample failed... returned not of type nii"

Expand Down
27 changes: 21 additions & 6 deletions api/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@

add_pagination(siibra_api)

# Versioning for api endpoints
siibra_api = VersionedFastAPI(siibra_api)

templates = Jinja2Templates(directory="templates/")
siibra_api.mount("/static", StaticFiles(directory="static"), name="static")

# Allow Cors

origins = ["*"]
Expand Down Expand Up @@ -363,6 +357,13 @@ def exception_sapi(request: Request, exc: SapiBaseException):
general_logger.warning(f"Error handler: exception_sapi: {str(exc)}")
raise HTTPException(400, str(exc))

@siibra_api.exception_handler(IndexError)
async def handle_index_error(request: Request, exc: IndexError):
return JSONResponse(status_code=404, content={
"detail": "Cannot find item",
"error": str(exc)
})

@siibra_api.exception_handler(Exception)
async def exception_other(request: Request, exc: Exception):
"""Catch all exception handler"""
Expand All @@ -375,6 +376,20 @@ async def exception_other(request: Request, exc: Exception):
}
)

# Versioning for api endpoints
exception_handlers = siibra_api.exception_handlers
siibra_api = VersionedFastAPI(siibra_api)

# add error handlers to versioned fastapi
# see https://github.com/DeanWay/fastapi-versioning/issues/30
for sub_app in siibra_api.routes:
if hasattr(sub_app.app, "add_exception_handler"):
for klass, handler in exception_handlers.items():
sub_app.app.add_exception_handler(klass, handler)

# it seems other mounts must be mounted **after** VersionedFastAPI is called
templates = Jinja2Templates(directory="templates/")
siibra_api.mount("/static", StaticFiles(directory="static"), name="static")

# TODO lifespan not working properly. Fix and use lifespan in future
@siibra_api.on_event("shutdown")
Expand Down
4 changes: 2 additions & 2 deletions api/server/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_siibra_map(parcellation_id: str, space_id: str, map_type: MapType, name
""")
@version(*FASTAPI_VERSION)
@router_decorator(ROLE, func=old_get_resampled_map)
def get_resampled_map(parcellation_id: str, space_id: str, *, func):
def get_resampled_map(parcellation_id: str, space_id: str, name: str="", *, func):
"""Get resampled map according to specification"""
if func is None:
raise HTTPException(500, f"func: None passsed")
Expand All @@ -55,7 +55,7 @@ def get_resampled_map(parcellation_id: str, space_id: str, *, func):
"content-disposition": f'attachment; filename="labelled_map.nii.gz"'
}

full_filename, cache_flag = func(parcellation_id=parcellation_id, space_id=space_id)
full_filename, cache_flag = func(parcellation_id=parcellation_id, space_id=space_id, name=name)
if cache_flag:
headers[cache_header] = "hit"
assert os.path.isfile(full_filename), f"file saved incorrectly"
Expand Down

0 comments on commit 515468b

Please sign in to comment.