|
16 | 16 | _handled_exception_context, |
17 | 17 | _sort_exceptions_by_specificity, |
18 | 18 | create__http_error_map_handler, |
| 19 | + create_exception_handlers_decorator, |
19 | 20 | ) |
20 | 21 |
|
21 | 22 |
|
@@ -98,15 +99,50 @@ def _suppress_handler(exception, request): |
98 | 99 | ), "only BasePluginError exceptions should call this handler" |
99 | 100 | return None # noqa: RET501, PLR1711 |
100 | 101 |
|
101 | | - def _rest_handler(exc_cls): |
| 102 | + def _fun(raises): |
102 | 103 | with _handled_exception_context( |
103 | 104 | BasePluginError, _suppress_handler, request=fake_request |
104 | 105 | ): |
105 | | - raise exc_cls |
| 106 | + raise raises |
106 | 107 |
|
107 | 108 | # checks |
108 | | - _rest_handler(OneError) |
109 | | - _rest_handler(OtherError) |
| 109 | + _fun(raises=OneError) |
| 110 | + _fun(raises=OtherError) |
110 | 111 |
|
111 | 112 | with pytest.raises(ArithmeticError): |
112 | | - _rest_handler(ArithmeticError) |
| 113 | + _fun(raises=ArithmeticError) |
| 114 | + |
| 115 | + |
| 116 | +async def test_exception_handlers_decorator(): |
| 117 | + def _suppress_handler(exception, request): |
| 118 | + assert isinstance( |
| 119 | + exception, BasePluginError |
| 120 | + ), "only BasePluginError exceptions should call this handler" |
| 121 | + return None # noqa: RET501, PLR1711 |
| 122 | + |
| 123 | + _handle_exceptons = create_exception_handlers_decorator( |
| 124 | + _suppress_handler, BasePluginError |
| 125 | + ) |
| 126 | + |
| 127 | + @_handle_exceptons |
| 128 | + async def _rest_handler(request: web.Request): |
| 129 | + if request.query.get("raise") == "OneError": |
| 130 | + raise OneError |
| 131 | + if request.query.get("raise") == "ArithmeticError": |
| 132 | + raise ArithmeticError |
| 133 | + |
| 134 | + return web.Response(text="all good") |
| 135 | + |
| 136 | + # emulates call |
| 137 | + resp = await _rest_handler(make_mocked_request("GET", "/foo")) |
| 138 | + assert resp.status == status.HTTP_200_OK |
| 139 | + |
| 140 | + # OMG! not good!? |
| 141 | + resp = await _rest_handler(make_mocked_request("GET", "/foo?raise=OneError")) |
| 142 | + assert resp is None |
| 143 | + |
| 144 | + # typically capture by last |
| 145 | + with pytest.raises(ArithmeticError): |
| 146 | + resp = await _rest_handler( |
| 147 | + make_mocked_request("GET", "/foo?raise=ArithmeticError") |
| 148 | + ) |
0 commit comments