Skip to content

Commit 86f90a2

Browse files
committed
🐛 Fix error handling to ensure proper JSON response structure and add tests for raw text responses
1 parent 125e5f5 commit 86f90a2

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def handle_validation_as_http_error(
6767
}
6868
for e in details
6969
]
70-
error_str = json_dumps(
70+
error_json_str = json_dumps(
7171
{
7272
"error": {
7373
"status": status.HTTP_422_UNPROCESSABLE_ENTITY,
@@ -77,7 +77,7 @@ def handle_validation_as_http_error(
7777
)
7878
else:
7979
# NEW proposed error for https://github.com/ITISFoundation/osparc-simcore/issues/443
80-
error_str = json_dumps(
80+
error_json_str = json_dumps(
8181
{
8282
"error": {
8383
"msg": user_error_message,
@@ -88,7 +88,7 @@ def handle_validation_as_http_error(
8888
)
8989

9090
raise web.HTTPUnprocessableEntity( # 422
91-
text=error_str,
91+
text=error_json_str,
9292
content_type=MIMETYPE_APPLICATION_JSON,
9393
) from err
9494

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from aiohttp.web_request import Request
1313
from aiohttp.web_response import StreamResponse
1414
from common_library.error_codes import ErrorCodeStr, create_error_code
15-
from common_library.json_serialization import json_dumps
15+
from common_library.json_serialization import json_dumps, json_loads
1616
from common_library.user_messages import user_message
1717
from models_library.basic_types import IDStr
1818
from models_library.rest_error import ErrorGet, ErrorItemType, LogMessageType
@@ -168,8 +168,9 @@ def _handle_http_successful(
168168
)
169169

170170
if exception.text and not is_enveloped_from_text(exception.text):
171-
payload = wrap_as_envelope(data=exception.text)
172-
exception.text = json_dumps(payload)
171+
# Ensures that the response is enveloped
172+
data = json_loads(exception.text)
173+
exception.text = json_dumps({"data": data})
173174

174175
return exception
175176

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ async def raise_success_with_text(_request: web.Request):
128128
# NOTE: explicitly NOT enveloped!
129129
raise web.HTTPOk(reason="I'm ok", text=json.dumps({"ok": True}))
130130

131+
@staticmethod
132+
async def raise_success_with_raw_text(_request: web.Request):
133+
raise web.HTTPOk(text="I'm ok") # NOT ALLOWED!
134+
131135

132136
@pytest.fixture
133137
async def client(
@@ -158,6 +162,10 @@ async def client(
158162
("/v1/raise_success", Handlers.raise_success),
159163
("/v1/raise_success_with_reason", Handlers.raise_success_with_reason),
160164
("/v1/raise_success_with_text", Handlers.raise_success_with_text),
165+
(
166+
"/v1/raise_success_with_raw_text",
167+
Handlers.raise_success_with_raw_text,
168+
),
161169
]
162170
]
163171
)
@@ -330,6 +338,7 @@ async def test_http_ok_with_text_is_enveloped(client: TestClient):
330338
"""Test that HTTPOk with text is properly enveloped."""
331339
response = await client.get("/v1/raise_success_with_text")
332340
assert response.status == status.HTTP_200_OK
341+
assert response.reason == "I'm ok"
333342

334343
# Should be enveloped
335344
payload = await response.json()
@@ -342,6 +351,11 @@ async def test_http_ok_with_text_is_enveloped(client: TestClient):
342351
assert data.get("ok") is True
343352

344353

354+
async def test_http_ok_with_raw_text_is_not_allowed(client: TestClient):
355+
response = await client.get("/v1/raise_success_with_raw_text")
356+
assert response.status == status.HTTP_500_INTERNAL_SERVER_ERROR
357+
358+
345359
async def test_exception_in_handler_returns_500(
346360
client: TestClient, mocker: MockerFixture
347361
):

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from models_library.api_schemas_webserver.activity import ActivityStatusDict
77
from pydantic import TypeAdapter
88
from servicelib.aiohttp.client_session import get_client_session
9-
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
109
from servicelib.request_keys import RQT_USERID_KEY
1110
from yarl import URL
1211

0 commit comments

Comments
 (0)