|
57 | 57 |
|
58 | 58 | add_pagination(siibra_api)
|
59 | 59 |
|
| 60 | +@siibra_api.exception_handler(RuntimeError) |
| 61 | +async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse: |
| 62 | + """Handling RuntimeErrors. |
| 63 | + Most of the RuntimeErrors are thrown by the siibra-python library when other Services are not responding. |
| 64 | + To be more resilient and not throw a simple and unplanned HTTP 500 response, this handler will return an HTTP 503 |
| 65 | + status.""" |
| 66 | + general_logger.warning(f"Error handler: exception_runtime: {str(exc)}") |
| 67 | + return JSONResponse( |
| 68 | + status_code=503, |
| 69 | + content={ |
| 70 | + "detail": "This part of the siibra service is temporarily unavailable", |
| 71 | + "error": str(exc) |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@siibra_api.exception_handler(SapiBaseException) |
| 77 | +def exception_sapi(request: Request, exc: SapiBaseException): |
| 78 | + """Handle sapi errors""" |
| 79 | + general_logger.warning(f"Error handler: exception_sapi: {str(exc)}") |
| 80 | + raise HTTPException(400, str(exc)) |
| 81 | + |
| 82 | +@siibra_api.exception_handler(IndexError) |
| 83 | +async def handle_index_error(request: Request, exc: IndexError): |
| 84 | + return JSONResponse(status_code=404, content={ |
| 85 | + "detail": "Cannot find item", |
| 86 | + "error": str(exc) |
| 87 | + }) |
| 88 | + |
| 89 | +@siibra_api.exception_handler(Exception) |
| 90 | +async def exception_other(request: Request, exc: Exception): |
| 91 | + """Catch all exception handler""" |
| 92 | + general_logger.warning(f"Error handler: exception_other: {str(exc)}") |
| 93 | + return JSONResponse( |
| 94 | + status_code=500, |
| 95 | + content={ |
| 96 | + "detail": "Some error occurred", |
| 97 | + "error": str(exc) |
| 98 | + } |
| 99 | + ) |
| 100 | + |
| 101 | +# Versioning for api endpoints |
| 102 | +exception_handlers = siibra_api.exception_handlers |
| 103 | +siibra_api = VersionedFastAPI(siibra_api) |
| 104 | + |
| 105 | + |
60 | 106 | # Allow Cors
|
61 | 107 |
|
62 | 108 | origins = ["*"]
|
@@ -247,8 +293,6 @@ async def middleware_add_version_header(request: Request, call_next):
|
247 | 293 | @siibra_api.middleware("http")
|
248 | 294 | async def middleware_access_log(request: Request, call_next):
|
249 | 295 | """Access log middleware"""
|
250 |
| - |
251 |
| - print("AccessLog middleware called") |
252 | 296 |
|
253 | 297 | if request.url.path in do_not_logs:
|
254 | 298 | return await call_next(request)
|
@@ -290,50 +334,6 @@ async def append_origin_header(request: Request, call_next):
|
290 | 334 | request.scope["headers"] = new_headers
|
291 | 335 | return await call_next(request)
|
292 | 336 |
|
293 |
| -@siibra_api.exception_handler(RuntimeError) |
294 |
| -async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse: |
295 |
| - """Handling RuntimeErrors. |
296 |
| - Most of the RuntimeErrors are thrown by the siibra-python library when other Services are not responding. |
297 |
| - To be more resilient and not throw a simple and unplanned HTTP 500 response, this handler will return an HTTP 503 |
298 |
| - status.""" |
299 |
| - general_logger.warning(f"Error handler: exception_runtime: {str(exc)}") |
300 |
| - return JSONResponse( |
301 |
| - status_code=503, |
302 |
| - content={ |
303 |
| - "detail": "This part of the siibra service is temporarily unavailable", |
304 |
| - "error": str(exc) |
305 |
| - }, |
306 |
| - ) |
307 |
| - |
308 |
| - |
309 |
| -@siibra_api.exception_handler(SapiBaseException) |
310 |
| -def exception_sapi(request: Request, exc: SapiBaseException): |
311 |
| - """Handle sapi errors""" |
312 |
| - general_logger.warning(f"Error handler: exception_sapi: {str(exc)}") |
313 |
| - raise HTTPException(400, str(exc)) |
314 |
| - |
315 |
| -@siibra_api.exception_handler(IndexError) |
316 |
| -async def handle_index_error(request: Request, exc: IndexError): |
317 |
| - return JSONResponse(status_code=404, content={ |
318 |
| - "detail": "Cannot find item", |
319 |
| - "error": str(exc) |
320 |
| - }) |
321 |
| - |
322 |
| -@siibra_api.exception_handler(Exception) |
323 |
| -async def exception_other(request: Request, exc: Exception): |
324 |
| - """Catch all exception handler""" |
325 |
| - general_logger.warning(f"Error handler: exception_other: {str(exc)}") |
326 |
| - return JSONResponse( |
327 |
| - status_code=500, |
328 |
| - content={ |
329 |
| - "detail": "Some error occurred", |
330 |
| - "error": str(exc) |
331 |
| - } |
332 |
| - ) |
333 |
| - |
334 |
| -# Versioning for api endpoints |
335 |
| -exception_handlers = siibra_api.exception_handlers |
336 |
| -siibra_api = VersionedFastAPI(siibra_api) |
337 | 337 |
|
338 | 338 | # add error handlers to versioned fastapi
|
339 | 339 | # see https://github.com/DeanWay/fastapi-versioning/issues/30
|
|
0 commit comments