Skip to content

Commit 7d17535

Browse files
committed
tests
1 parent e378c1a commit 7d17535

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

services/web/server/tests/unit/isolated/test_exceptions_handlers.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_handled_exception_context,
1717
_sort_exceptions_by_specificity,
1818
create__http_error_map_handler,
19+
create_exception_handlers_decorator,
1920
)
2021

2122

@@ -98,15 +99,50 @@ def _suppress_handler(exception, request):
9899
), "only BasePluginError exceptions should call this handler"
99100
return None # noqa: RET501, PLR1711
100101

101-
def _rest_handler(exc_cls):
102+
def _fun(raises):
102103
with _handled_exception_context(
103104
BasePluginError, _suppress_handler, request=fake_request
104105
):
105-
raise exc_cls
106+
raise raises
106107

107108
# checks
108-
_rest_handler(OneError)
109-
_rest_handler(OtherError)
109+
_fun(raises=OneError)
110+
_fun(raises=OtherError)
110111

111112
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

Comments
 (0)