|
| 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 servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON |
| 14 | +from simcore_service_webserver.errors import WebServerBaseError |
| 15 | +from simcore_service_webserver.exceptions_handlers_base import ( |
| 16 | + AsyncDynamicTryExceptContext, |
| 17 | +) |
| 18 | +from simcore_service_webserver.exceptions_handlers_http_error_map import ( |
| 19 | + ExceptionToHttpErrorMap, |
| 20 | + HttpErrorInfo, |
| 21 | + create_exception_handler_from_http_error, |
| 22 | + to_exceptions_handlers_map, |
| 23 | +) |
| 24 | + |
| 25 | +# Some custom errors in my service |
| 26 | + |
| 27 | + |
| 28 | +class BaseError(WebServerBaseError): |
| 29 | + ... |
| 30 | + |
| 31 | + |
| 32 | +class OneError(BaseError): |
| 33 | + ... |
| 34 | + |
| 35 | + |
| 36 | +class OtherError(BaseError): |
| 37 | + ... |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def fake_request() -> web.Request: |
| 42 | + return make_mocked_request("GET", "/foo") |
| 43 | + |
| 44 | + |
| 45 | +async def test_factory__create_exception_handler_from_http_error( |
| 46 | + fake_request: web.Request, |
| 47 | +): |
| 48 | + one_error_to_404 = create_exception_handler_from_http_error( |
| 49 | + status_code=status.HTTP_404_NOT_FOUND, |
| 50 | + msg_template="one error message for the user: {code} {value}", |
| 51 | + ) |
| 52 | + |
| 53 | + # calling exception handler |
| 54 | + caught = OneError() |
| 55 | + response = await one_error_to_404(fake_request, caught) |
| 56 | + assert response.status == status.HTTP_404_NOT_FOUND |
| 57 | + assert response.text is not None |
| 58 | + assert "one error message" in response.reason |
| 59 | + assert response.content_type == MIMETYPE_APPLICATION_JSON |
| 60 | + |
| 61 | + |
| 62 | +async def test_factory__create_exception_handler_from_http_error_map( |
| 63 | + fake_request: web.Request, |
| 64 | +): |
| 65 | + exc_to_http_error_map: ExceptionToHttpErrorMap = { |
| 66 | + OneError: HttpErrorInfo(status.HTTP_400_BAD_REQUEST, "Error One mapped to 400") |
| 67 | + } |
| 68 | + |
| 69 | + cm = AsyncDynamicTryExceptContext( |
| 70 | + to_exceptions_handlers_map(exc_to_http_error_map), request=fake_request |
| 71 | + ) |
| 72 | + async with cm: |
| 73 | + raise OneError |
| 74 | + |
| 75 | + response = cm.get_response() |
| 76 | + assert response is not None |
| 77 | + assert response.status == status.HTTP_400_BAD_REQUEST |
| 78 | + assert response.reason == "Error One mapped to 400" |
| 79 | + |
| 80 | + # By-passes exceptions not listed |
| 81 | + err = RuntimeError() |
| 82 | + with pytest.raises(RuntimeError) as err_info: |
| 83 | + async with cm: |
| 84 | + raise err |
| 85 | + |
| 86 | + assert cm.get_response() is None |
| 87 | + assert err_info.value == err |
| 88 | + |
| 89 | + |
| 90 | +# async def test__handled_exception_context_manager(fake_request: web.Request): |
| 91 | + |
| 92 | +# expected_response = web.json_response({"error": {"msg": "Foo"}}) |
| 93 | + |
| 94 | +# async def _custom_handler(request, exception): |
| 95 | +# assert request == fake_request |
| 96 | +# assert isinstance( |
| 97 | +# exception, BaseError |
| 98 | +# ), "only BasePluginError exceptions should call this handler" |
| 99 | +# return expected_response |
| 100 | + |
| 101 | +# exc_handling_ctx = _handled_exception_context_manager( |
| 102 | +# BaseError, _custom_handler, request=fake_request |
| 103 | +# ) |
| 104 | + |
| 105 | +# # handles any BaseError returning a response |
| 106 | +# async with exc_handling_ctx as ctx: |
| 107 | +# raise OneError |
| 108 | +# assert ctx.response == expected_response |
| 109 | + |
| 110 | +# async with exc_handling_ctx as ctx: |
| 111 | +# raise OtherError |
| 112 | +# assert ctx.response == expected_response |
| 113 | + |
| 114 | +# # otherwise thru |
| 115 | +# with pytest.raises(ArithmeticError): |
| 116 | +# async with exc_handling_ctx: |
| 117 | +# raise ArithmeticError |
| 118 | + |
| 119 | + |
| 120 | +# async def test_create_decorator_from_exception_handler( |
| 121 | +# caplog: pytest.LogCaptureFixture, |
| 122 | +# ): |
| 123 | +# # Create an SINGLE exception handler that acts as umbrella for all these exceptions |
| 124 | +# http_error_map: ExceptionToHttpErrorMap = { |
| 125 | +# OneError: HttpErrorInfo( |
| 126 | +# status.HTTP_503_SERVICE_UNAVAILABLE, |
| 127 | +# "Human readable error transmitted to the front-end", |
| 128 | +# ) |
| 129 | +# } |
| 130 | + |
| 131 | +# exc_handler = create_exception_handler_from_http_error_map(http_error_map) |
| 132 | +# _exc_handling_ctx = create_decorator_from_exception_handler( |
| 133 | +# exception_types=BaseError, # <--- FIXME" this is redundant because exception has been already passed in exc_handler! |
| 134 | +# exception_handler=exc_handler, |
| 135 | +# ) |
| 136 | + |
| 137 | +# @_exc_handling_ctx |
| 138 | +# async def _rest_handler(request: web.Request) -> web.Response: |
| 139 | +# if request.query.get("raise") == "OneError": |
| 140 | +# raise OneError |
| 141 | +# if request.query.get("raise") == "ArithmeticError": |
| 142 | +# raise ArithmeticError |
| 143 | + |
| 144 | +# return web.Response(reason="all good") |
| 145 | + |
| 146 | +# with caplog.at_level(logging.ERROR): |
| 147 | + |
| 148 | +# # emulates successful call |
| 149 | +# resp = await _rest_handler(make_mocked_request("GET", "/foo")) |
| 150 | +# assert resp.status == status.HTTP_200_OK |
| 151 | +# assert resp.reason == "all good" |
| 152 | + |
| 153 | +# assert not caplog.records |
| 154 | + |
| 155 | +# # this will be passed and catched by the outermost error middleware |
| 156 | +# with pytest.raises(ArithmeticError): |
| 157 | +# await _rest_handler( |
| 158 | +# make_mocked_request("GET", "/foo?raise=ArithmeticError") |
| 159 | +# ) |
| 160 | + |
| 161 | +# assert not caplog.records |
| 162 | + |
| 163 | +# # this is a 5XX will be converted to response but is logged as error as well |
| 164 | +# with pytest.raises(web.HTTPException) as exc_info: |
| 165 | +# await _rest_handler(make_mocked_request("GET", "/foo?raise=OneError")) |
| 166 | + |
| 167 | +# resp = exc_info.value |
| 168 | +# assert resp.status == status.HTTP_503_SERVICE_UNAVAILABLE |
| 169 | +# assert "front-end" in resp.reason |
| 170 | + |
| 171 | +# assert caplog.records, "Expected 5XX troubleshooting logged as error" |
| 172 | +# assert caplog.records[0].levelno == logging.ERROR |
| 173 | + |
| 174 | +# # typically capture by last |
| 175 | +# with pytest.raises(ArithmeticError): |
| 176 | +# resp = await _rest_handler( |
| 177 | +# make_mocked_request("GET", "/foo?raise=ArithmeticError") |
| 178 | +# ) |
0 commit comments