-
Notifications
You must be signed in to change notification settings - Fork 27
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
🎨 web-server: exception handling framework #6655
🎨 web-server: exception handling framework #6655
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6655 +/- ##
==========================================
- Coverage 88.04% 82.09% -5.95%
==========================================
Files 1550 625 -925
Lines 61769 31104 -30665
Branches 2110 263 -1847
==========================================
- Hits 54382 25536 -28846
+ Misses 7056 5508 -1548
+ Partials 331 60 -271
Continue to review full report in Codecov by Sentry.
|
f8e744c
to
f819ffc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nice. please have a look at my comments. thanks
services/web/server/src/simcore_service_webserver/exception_handling.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
services/web/server/src/simcore_service_webserver/exception_handling_base.py
Outdated
Show resolved
Hide resolved
391bc19
to
c275789
Compare
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! This is definitely a great improvement and a step in the right direction.
) -> list[type[Exception]]: | ||
""" | ||
Keyword Arguments: | ||
concrete_first -- If True, concrete subclasses precede their superclass (default: {True}). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have named this subclasses_first
. It is not obvious to me what concrete_first
does
None, | ||
) | ||
): | ||
exc_handler = self._exc_handlers_map[base_exc_type] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I am late to the party (sorry). But I am wondering if you profit from the sorting and traversing the exception types 🤔.
Python has a builtin way to resolve inherritance and you could use that to only traverse the super-types of an exception
exc_handler = None
for type_ in exc_type.__mro__:
if exc_handler := self._exc_handlers_map.get(type_):
break
return exc_handler
async def middleware_handler(request: web.Request, handler: WebHandler): | ||
return await _handle_excs(handler)(request) | ||
|
||
return middleware_handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Starlette/FastAPI approach to exception handlers (using this type of middleware) is quite elegant, but what always confuses me is that when I raise somewhere I don't see if and where that exception will be handled. For that I have to manually "follow" the exception up until I see it reaches the middleware. Perhaps it would make sense to have an easy way to test that a handler for a specific exception type is already handled. And maybe even ensure you cannot add two handlers for the same exception type. But I guess this is closely related to the general question of the python exception mechanism: I cannot know what a given function will raise unless I look into the code.
What do these changes do?
This PR introduces an exception handling framework for the web-server service.
simcore_service_webserver.exception_handling
._TO_HTTP_ERROR_MAP
already used in some plugins of thewebserver
, e.g. in_folders
These are the highlights:
Inspired by Starlette's exception handlers, this implementation adapts the concept to
aiohttp
. Exception handlers are callables that process exceptions and determine the appropriate response to return when errors or handled exceptions occur.The following demonstrates how exception handlers (
exc_handler
) integrate into the HTTP request-response lifecycle:Map Exception-HttpInfo
The PR includes tools to simplify the creation of exception handlers using mappings from exceptions to HTTP info that include a status code and a message. See
simcore_service_webserver.{plugin}._exception_handlers._TO_HTTP_ERROR_MAP
for details.Multiple Integration Options
Provides flexibility by supporting exception handling via:
Related issue/s
How to test
Dev-ops
None