Skip to content

Commit b086afc

Browse files
committed
adds model policy
1 parent e3c6aac commit b086afc

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ def _process_and_raise_unexpected_error(request: web.BaseRequest, err: Exception
5151
"request.path": f"{request.path}",
5252
}
5353

54-
user_error_msg = _FMSG_INTERNAL_ERROR_USER_FRIENDLY.format(
55-
error_code=error_code
56-
)
54+
user_error_msg = _FMSG_INTERNAL_ERROR_USER_FRIENDLY
5755
http_error = create_http_error(
5856
err,
5957
user_error_msg,
6058
web.HTTPInternalServerError,
6159
skip_internal_error_details=_is_prod,
60+
error_code=error_code,
6261
)
6362
_logger.exception(
6463
**create_troubleshotting_log_kwargs(

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from common_library.json_serialization import json_dumps
1010
from models_library.basic_types import IDStr
1111
from models_library.rest_error import ErrorGet, ErrorItemType
12+
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
1213

1314
from ..aiohttp.status import HTTP_200_OK
1415
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
@@ -85,11 +86,15 @@ def create_http_error(
8586
)
8687

8788
assert not http_error_cls.empty_body # nosec
88-
payload = wrap_as_envelope(error=error)
89+
payload = wrap_as_envelope(
90+
error=error.model_dump(mode="json", **RESPONSE_MODEL_POLICY)
91+
)
8992

9093
return http_error_cls(
9194
reason=reason,
92-
text=json_dumps(payload),
95+
text=json_dumps(
96+
payload,
97+
),
9398
content_type=MIMETYPE_APPLICATION_JSON,
9499
)
95100

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pylint: disable=unused-variable
44

55
import itertools
6+
import json
67

78
import pytest
89
from aiohttp import web
@@ -15,6 +16,7 @@
1516
HTTPNotModified,
1617
HTTPOk,
1718
)
19+
from common_library.error_codes import ErrorCodeStr, create_error_code
1820
from servicelib.aiohttp import status
1921
from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
2022
from servicelib.aiohttp.web_exceptions_extension import (
@@ -58,26 +60,40 @@ def test_collected_http_errors_map(status_code: int, http_error_cls: type[HTTPEr
5860

5961

6062
@pytest.mark.parametrize("skip_details", [True, False])
61-
def tests_exception_to_response(skip_details: bool):
62-
exception = create_http_error(
63-
errors=[RuntimeError("foo")],
64-
reason="Something whent wrong",
63+
@pytest.mark.parametrize("error_code", [None, create_error_code(Exception("fake"))])
64+
def tests_exception_to_response(skip_details: bool, error_code: ErrorCodeStr | None):
65+
66+
expected_reason = "Something whent wrong !"
67+
expected_exceptions: list[Exception] = [RuntimeError("foo")]
68+
69+
http_error = create_http_error(
70+
errors=expected_exceptions,
71+
reason=expected_reason,
6572
http_error_cls=web.HTTPInternalServerError,
6673
skip_internal_error_details=skip_details,
74+
error_code=error_code,
6775
)
6876

6977
# For now until deprecated SEE https://github.com/aio-libs/aiohttp/issues/2415
70-
assert isinstance(exception, Exception)
71-
assert isinstance(exception, web.Response)
72-
assert hasattr(exception, "__http_exception__")
78+
assert isinstance(http_error, Exception)
79+
assert isinstance(http_error, web.Response)
80+
assert hasattr(http_error, "__http_exception__")
7381

7482
# until they have exception.make_response(), we user
75-
response = exception_to_response(exception)
83+
response = exception_to_response(http_error)
7684
assert isinstance(response, web.Response)
7785
assert not isinstance(response, Exception)
7886
assert not hasattr(response, "__http_exception__")
7987

88+
# checks response components
8089
assert response.content_type == MIMETYPE_APPLICATION_JSON
8190
assert response.status == status.HTTP_500_INTERNAL_SERVER_ERROR
8291
assert response.text
8392
assert response.body
93+
94+
# checks response model
95+
response_json = json.loads(response.text)
96+
assert response_json["data"] is None
97+
assert response_json["error"]["message"] == expected_reason
98+
assert response_json["error"]["supportId"] == error_code
99+
assert response_json["error"]["status"] == response.status

0 commit comments

Comments
 (0)