Skip to content

Commit 6d2901d

Browse files
committed
mv functions from rest-responses to web-exceptions
1 parent fa0bba3 commit 6d2901d

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed

packages/service-library/src/servicelib/aiohttp/rest_responses.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,3 @@ def _pred(obj) -> bool:
151151
assert len(http_statuses) == len(found), "No duplicates" # nosec
152152

153153
return http_statuses
154-
155-
156-
_STATUS_CODE_TO_HTTP_ERRORS: dict[int, type[HTTPError]] = _collect_http_exceptions(
157-
HTTPError
158-
)
159-
160-
161-
def get_http_error(status_code: int) -> type[HTTPError] | None:
162-
"""Returns aiohttp error class corresponding to a 4XX or 5XX status code
163-
164-
NOTICE that any non-error code (i.e. 2XX, 3XX and 4XX) will return None
165-
"""
166-
return _STATUS_CODE_TO_HTTP_ERRORS.get(status_code)

packages/service-library/src/servicelib/aiohttp/web_exceptions_extension.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
""" Extends `aiohttp.web_exceptions` classes to match `status` codes
2+
and adds helper functions.
3+
"""
4+
15
import inspect
26
from typing import Any
37

packages/service-library/tests/aiohttp/test_rest_responses.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
HTTPOk,
1717
)
1818
from servicelib.aiohttp import status
19-
from servicelib.aiohttp.rest_responses import (
19+
from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
20+
from servicelib.aiohttp.web_exceptions_extension import (
2021
_STATUS_CODE_TO_HTTP_ERRORS,
21-
create_http_error,
22-
exception_to_response,
23-
get_http_error,
22+
get_http_error_class_or_none,
2423
)
2524
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
2625

@@ -36,17 +35,17 @@
3635

3736

3837
@pytest.mark.parametrize(
39-
"http_exc", (HTTPBadRequest, HTTPGone, HTTPInternalServerError)
38+
"http_exc", [HTTPBadRequest, HTTPGone, HTTPInternalServerError]
4039
)
4140
def test_get_http_exception_class_from_code(http_exc: HTTPException):
42-
assert get_http_error(http_exc.status_code) == http_exc
41+
assert get_http_error_class_or_none(http_exc.status_code) == http_exc
4342

4443

4544
@pytest.mark.parametrize(
4645
"status_code", itertools.chain(BELOW_1XX, NONE_ERRORS, ABOVE_599)
4746
)
4847
def test_get_none_for_invalid_or_not_errors_code(status_code):
49-
assert get_http_error(status_code) is None
48+
assert get_http_error_class_or_none(status_code) is None
5049

5150

5251
@pytest.mark.parametrize(

services/web/server/src/simcore_service_webserver/director_v2/_handlers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
from models_library.utils.json_serialization import json_dumps
1111
from pydantic import BaseModel, Field, ValidationError, parse_obj_as
1212
from pydantic.types import NonNegativeInt
13-
from servicelib.aiohttp.rest_responses import (
14-
create_http_error,
15-
exception_to_response,
16-
get_http_error,
17-
)
13+
from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
14+
from servicelib.aiohttp.web_exceptions_extension import get_http_error_class_or_none
1815
from servicelib.common_headers import (
1916
UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE,
2017
X_SIMCORE_USER_AGENT,
@@ -170,7 +167,8 @@ async def start_computation(request: web.Request) -> web.Response:
170167
create_http_error(
171168
exc,
172169
reason=exc.reason,
173-
http_error_cls=get_http_error(exc.status) or web.HTTPServiceUnavailable,
170+
http_error_cls=get_http_error_class_or_none(exc.status)
171+
or web.HTTPServiceUnavailable,
174172
)
175173
)
176174
except UserDefaultWalletNotFoundError as exc:
@@ -210,7 +208,8 @@ async def stop_computation(request: web.Request) -> web.Response:
210208
return create_http_error(
211209
exc,
212210
reason=exc.reason,
213-
http_error_cls=get_http_error(exc.status) or web.HTTPServiceUnavailable,
211+
http_error_cls=get_http_error_class_or_none(exc.status)
212+
or web.HTTPServiceUnavailable,
214213
)
215214

216215

@@ -259,7 +258,8 @@ async def get_computation(request: web.Request) -> web.Response:
259258
return create_http_error(
260259
exc,
261260
reason=exc.reason,
262-
http_error_cls=get_http_error(exc.status) or web.HTTPServiceUnavailable,
261+
http_error_cls=get_http_error_class_or_none(exc.status)
262+
or web.HTTPServiceUnavailable,
263263
)
264264
except ValidationError as exc:
265265
return create_http_error(exc, http_error_cls=web.HTTPInternalServerError)

0 commit comments

Comments
 (0)