Skip to content

Commit dc0b74d

Browse files
committed
šŸ› Refactor error handling to use safe_status_message for HTTP reason attributes in exception responses
1 parent 5b5a3d8 commit dc0b74d

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

ā€Žservices/web/server/src/simcore_service_webserver/exception_handling/_factory.pyā€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from common_library.error_codes import create_error_code
66
from common_library.json_serialization import json_dumps
77
from models_library.rest_error import ErrorGet
8+
from servicelib.aiohttp.rest_responses import safe_status_message
89
from servicelib.aiohttp.web_exceptions_extension import get_all_aiohttp_http_exceptions
910
from servicelib.logging_errors import create_troubleshotting_log_kwargs
1011
from servicelib.status_codes_utils import is_5xx_server_error, is_error
@@ -40,8 +41,7 @@ def create_error_response(error: ErrorGet, status_code: int) -> web.Response:
4041
return web.json_response(
4142
data={"error": error.model_dump(exclude_unset=True, mode="json")},
4243
dumps=json_dumps,
43-
# NOTE: Multiline not allowed in HTTP reason attribute (aiohttp now raises ValueError)
44-
reason=error.message.replace("\n", " ") if error.message else None,
44+
reason=safe_status_message(error.message),
4545
status=status_code,
4646
)
4747

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from aiohttp.web import HTTPBadRequest
2+
from servicelib.aiohttp.rest_responses import safe_status_message
23

34

45
class SDSException(HTTPBadRequest): # pylint: disable=too-many-ancestors
56
# NOTE: all these errors will displayed to the user
67
"""Basic exception for errors raised inside the module"""
78

89
def __init__(self, message: str):
9-
# Multiline not allowed in HTTP reason attribute
10-
super().__init__(reason=message.replace("\n", " ") if message else None)
10+
super().__init__(reason=safe_status_message(message))

ā€Žservices/web/server/src/simcore_service_webserver/exporter/utils.pyā€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from aiohttp.abc import AbstractStreamWriter
77
from aiohttp.typedefs import LooseHeaders
88
from aiohttp.web import BaseRequest, FileResponse
9+
from servicelib.aiohttp.rest_responses import safe_status_message
910

1011

1112
class CleanupFileResponse(FileResponse): # pylint: disable=too-many-ancestors
@@ -27,8 +28,7 @@ def __init__(
2728
path=path,
2829
chunk_size=chunk_size,
2930
status=status,
30-
# Multiline not allowed in HTTP reason
31-
reason=reason.replace("\n", " ") if reason else None,
31+
reason=safe_status_message(reason),
3232
headers=headers,
3333
)
3434
self.remove_tmp_dir_cb = remove_tmp_dir_cb

0 commit comments

Comments
Ā (0)