|
| 1 | +# pylint: disable=protected-access |
| 2 | +# pylint: disable=redefined-outer-name |
| 3 | +# pylint: disable=too-many-arguments |
| 4 | +# pylint: disable=too-many-statements |
| 5 | +# pylint: disable=unused-argument |
| 6 | +# pylint: disable=unused-variable |
| 7 | + |
| 8 | + |
| 9 | +import pytest |
| 10 | +from aiohttp import web |
| 11 | +from aiohttp.test_utils import make_mocked_request |
| 12 | +from servicelib.aiohttp import status |
| 13 | +from simcore_service_webserver.errors import WebServerBaseError |
| 14 | +from simcore_service_webserver.exceptions_handlers import ( |
| 15 | + HttpErrorInfo, |
| 16 | + _handled_exception_context, |
| 17 | + _sort_exceptions_by_specificity, |
| 18 | + create__http_error_map_handler, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class BasePluginError(WebServerBaseError): |
| 23 | + ... |
| 24 | + |
| 25 | + |
| 26 | +class OneError(BasePluginError): |
| 27 | + ... |
| 28 | + |
| 29 | + |
| 30 | +class OtherError(BasePluginError): |
| 31 | + ... |
| 32 | + |
| 33 | + |
| 34 | +def test_sort_concrete_first(): |
| 35 | + assert _sort_exceptions_by_specificity([Exception, BasePluginError]) == [ |
| 36 | + BasePluginError, |
| 37 | + Exception, |
| 38 | + ] |
| 39 | + |
| 40 | + assert _sort_exceptions_by_specificity( |
| 41 | + [Exception, BasePluginError], concrete_first=False |
| 42 | + ) == [ |
| 43 | + Exception, |
| 44 | + BasePluginError, |
| 45 | + ] |
| 46 | + |
| 47 | + |
| 48 | +def test_sort_exceptions_by_specificity(): |
| 49 | + |
| 50 | + got_exceptions_cls = _sort_exceptions_by_specificity( |
| 51 | + [ |
| 52 | + Exception, |
| 53 | + OtherError, |
| 54 | + OneError, |
| 55 | + BasePluginError, |
| 56 | + ValueError, |
| 57 | + ArithmeticError, |
| 58 | + ZeroDivisionError, |
| 59 | + ] |
| 60 | + ) |
| 61 | + |
| 62 | + for from_, exc in enumerate(got_exceptions_cls, start=1): |
| 63 | + for exc_after in got_exceptions_cls[from_:]: |
| 64 | + assert not issubclass(exc_after, exc), f"{got_exceptions_cls=}" |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def fake_request() -> web.Request: |
| 69 | + return make_mocked_request("GET", "/foo") |
| 70 | + |
| 71 | + |
| 72 | +def test_http_error_map_handler_factory(fake_request: web.Request): |
| 73 | + |
| 74 | + exc_handler = create__http_error_map_handler( |
| 75 | + { |
| 76 | + OneError: HttpErrorInfo( |
| 77 | + status.HTTP_400_BAD_REQUEST, "Error One mapped to 400" |
| 78 | + ) |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + # Converts exception in map |
| 83 | + got_exc = exc_handler(OneError(), fake_request) |
| 84 | + |
| 85 | + assert isinstance(got_exc, web.HTTPBadRequest) |
| 86 | + assert got_exc.reason == "Error One mapped to 400" |
| 87 | + |
| 88 | + # By-passes exceptions not listed |
| 89 | + err = RuntimeError() |
| 90 | + assert exc_handler(err, fake_request) is err |
| 91 | + |
| 92 | + |
| 93 | +def test_handled_exception_context(fake_request: web.Request): |
| 94 | + def _suppress_handler(exception, request): |
| 95 | + assert request == fake_request |
| 96 | + assert isinstance( |
| 97 | + exception, BasePluginError |
| 98 | + ), "only BasePluginError exceptions should call this handler" |
| 99 | + return None # noqa: RET501, PLR1711 |
| 100 | + |
| 101 | + def _rest_handler(exc_cls): |
| 102 | + with _handled_exception_context( |
| 103 | + BasePluginError, _suppress_handler, request=fake_request |
| 104 | + ): |
| 105 | + raise exc_cls |
| 106 | + |
| 107 | + # checks |
| 108 | + _rest_handler(OneError) |
| 109 | + _rest_handler(OtherError) |
| 110 | + |
| 111 | + with pytest.raises(ArithmeticError): |
| 112 | + _rest_handler(ArithmeticError) |
0 commit comments