Skip to content

Commit a3c81bb

Browse files
committed
🐛 Fix exception handling to use safe status messages and improve error response clarity
1 parent 03bc710 commit a3c81bb

File tree

2 files changed

+17
-8
lines changed
  • packages/service-library/src/servicelib/aiohttp
  • services/web/server/src/simcore_service_webserver/exception_handling

2 files changed

+17
-8
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,19 @@ def create_http_error(
133133
)
134134

135135

136-
def exception_to_response(exc: HTTPError) -> web.Response:
136+
def exception_to_response(exception: HTTPError) -> web.Response:
137137
# Returning web.HTTPException is deprecated so here we have a converter to a response
138138
# so it can be used as
139139
# SEE https://github.com/aio-libs/aiohttp/issues/2415
140+
141+
if exception.reason:
142+
reason = safe_status_message(exception.reason)
143+
else:
144+
reason = get_code_description(exception.status)
145+
140146
return web.Response(
141-
status=exc.status,
142-
headers=exc.headers,
143-
reason=exc.reason,
144-
text=exc.text,
147+
status=exception.status,
148+
headers=exception.headers,
149+
reason=reason,
150+
text=exception.text,
145151
)

services/web/server/src/simcore_service_webserver/exception_handling/_factory.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
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
98
from servicelib.aiohttp.web_exceptions_extension import get_all_aiohttp_http_exceptions
109
from servicelib.logging_errors import create_troubleshootting_log_kwargs
11-
from servicelib.status_codes_utils import is_5xx_server_error, is_error
10+
from servicelib.status_codes_utils import (
11+
get_code_description,
12+
is_5xx_server_error,
13+
is_error,
14+
)
1215

1316
from ._base import AiohttpExceptionHandler, ExceptionHandlersMap
1417

@@ -50,7 +53,7 @@ def create_error_response(error: ErrorGet, status_code: int) -> web.Response:
5053
return web.json_response(
5154
data={"error": error.model_dump(exclude_unset=True, mode="json")},
5255
dumps=json_dumps,
53-
reason=safe_status_message(error.message),
56+
reason=get_code_description(status_code),
5457
status=status_code,
5558
)
5659

0 commit comments

Comments
 (0)