|
7 | 7 |
|
8 | 8 | from collections.abc import Callable |
9 | 9 |
|
| 10 | +import pytest |
10 | 11 | from aiohttp import web |
| 12 | +from models_library.rest_error import ErrorGet |
11 | 13 | from servicelib.aiohttp import status |
12 | | -from simcore_service_webserver.exception_handling import exception_handling_decorator |
| 14 | +from simcore_service_webserver.exception_handling import ( |
| 15 | + ExceptionHandlersMap, |
| 16 | + HttpErrorInfo, |
| 17 | + exception_handling_decorator, |
| 18 | + to_exceptions_handlers_map, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def exception_handlers_map(build_method: str) -> ExceptionHandlersMap: |
| 24 | + """ |
| 25 | + Two different ways to build the exception_handlers_map |
| 26 | + """ |
| 27 | + exception_handlers_map: ExceptionHandlersMap = {} |
| 28 | + |
| 29 | + if build_method == "custom": |
| 30 | + |
| 31 | + async def _value_error_as_422( |
| 32 | + request: web.Request, exception: BaseException |
| 33 | + ) -> web.Response: |
| 34 | + # custom exception handler |
| 35 | + return web.json_response( |
| 36 | + reason=f"{build_method=}", status=status.HTTP_422_UNPROCESSABLE_ENTITY |
| 37 | + ) |
| 38 | + |
| 39 | + exception_handlers_map = { |
| 40 | + ValueError: _value_error_as_422, |
| 41 | + } |
| 42 | + |
| 43 | + elif build_method == "http_map": |
| 44 | + exception_handlers_map = to_exceptions_handlers_map( |
| 45 | + { |
| 46 | + ValueError: HttpErrorInfo( |
| 47 | + status.HTTP_422_UNPROCESSABLE_ENTITY, f"{build_method=}" |
| 48 | + ) |
| 49 | + } |
| 50 | + ) |
| 51 | + else: |
| 52 | + pytest.fail(f"Undefined {build_method=}") |
13 | 53 |
|
| 54 | + return exception_handlers_map |
14 | 55 |
|
15 | | -async def test_handling_exceptions_decorating_a_route(aiohttp_client: Callable): |
16 | 56 |
|
17 | | - # custom exception handler |
18 | | - async def _value_error_as_422( |
19 | | - request: web.Request, exception: BaseException |
20 | | - ) -> web.Response: |
21 | | - return web.json_response(status=status.HTTP_422_UNPROCESSABLE_ENTITY) |
| 57 | +@pytest.mark.parametrize("build_method", ["custom", "http_map"]) |
| 58 | +async def test_handling_exceptions_decorating_a_route( |
| 59 | + aiohttp_client: Callable, |
| 60 | + exception_handlers_map: ExceptionHandlersMap, |
| 61 | + build_method: str, |
| 62 | +): |
22 | 63 |
|
23 | 64 | # 1. create decorator |
24 | | - exc_handling = exception_handling_decorator( |
25 | | - exception_handlers_map={ |
26 | | - ValueError: _value_error_as_422, |
27 | | - } |
28 | | - ) |
| 65 | + exc_handling = exception_handling_decorator(exception_handlers_map) |
29 | 66 |
|
30 | 67 | # adding new routes |
31 | 68 | routes = web.RouteTableDef() |
@@ -61,6 +98,10 @@ async def _handler(request: web.Request): |
61 | 98 | # handled non-HTTPException exception |
62 | 99 | resp = await client.get("/ValueError") |
63 | 100 | assert resp.status == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 101 | + if build_method == "http_map": |
| 102 | + body = await resp.json() |
| 103 | + error = ErrorGet.model_validate(body["error"]) |
| 104 | + assert error.message == f"{build_method=}" |
64 | 105 |
|
65 | 106 | # undhandled non-HTTPException |
66 | 107 | resp = await client.get("/IndexError") |
|
0 commit comments