Skip to content

Commit dc66c13

Browse files
committed
refactors
1 parent a844f98 commit dc66c13

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ..logging_errors import create_troubleshotting_log_kwargs
2121
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
2222
from ..rest_responses import is_enveloped_from_map, is_enveloped_from_text
23+
from ..status_codes_utils import get_code_description
2324
from . import status
2425
from .rest_responses import (
2526
create_data_response,
@@ -46,21 +47,32 @@ def is_api_request(request: web.Request, api_version: str) -> bool:
4647
return bool(request.path.startswith(base_path))
4748

4849

49-
def _handle_unexpected_exception_as_500(
50-
request: web.BaseRequest,
51-
exception: Exception,
52-
) -> web.HTTPInternalServerError:
53-
"""Process unexpected exceptions and return them as HTTP errors with proper formatting.
50+
def _create_error_context(
51+
request: web.BaseRequest, exception: Exception
52+
) -> tuple[str, dict[str, Any]]:
53+
"""Create error code and context for logging purposes.
5454
55-
IMPORTANT: this function cannot throw exceptions, as it is called
55+
Returns:
56+
Tuple of (error_code, error_context)
5657
"""
5758
error_code = create_error_code(exception)
5859
error_context: dict[str, Any] = {
5960
"request.remote": f"{request.remote}",
6061
"request.method": f"{request.method}",
6162
"request.path": f"{request.path}",
6263
}
64+
return error_code, error_context
6365

66+
67+
def _handle_unexpected_exception_as_500(
68+
request: web.BaseRequest,
69+
exception: Exception,
70+
) -> web.HTTPInternalServerError:
71+
"""Process unexpected exceptions and return them as HTTP errors with proper formatting.
72+
73+
IMPORTANT: this function cannot throw exceptions, as it is called
74+
"""
75+
error_code, error_context = _create_error_context(request, exception)
6476
user_error_msg = _FMSG_INTERNAL_ERROR_USER_FRIENDLY
6577

6678
http_error = create_http_error(
@@ -113,12 +125,7 @@ def _handle_http_error(
113125
exception.text = EnvelopeFactory(error=error_model).as_text()
114126

115127
if is_5xx_server_error(exception.status):
116-
error_code = create_error_code(exception)
117-
error_context: dict[str, Any] = {
118-
"request.remote": f"{request.remote}",
119-
"request.method": f"{request.method}",
120-
"request.path": f"{request.path}",
121-
}
128+
error_code, error_context = _create_error_context(request, exception)
122129

123130
_logger.exception(
124131
**create_troubleshotting_log_kwargs(
@@ -171,13 +178,21 @@ def _handle_exception_as_http_error(
171178
)
172179
raise ValueError(msg)
173180

174-
error_message = f"{exception}" # FIXME: do not log exception message directly!!!!
181+
user_error_msg = get_code_description(status_code)
175182

176-
return create_http_error(
177-
exception,
178-
error_message,
179-
http_error_cls,
180-
)
183+
if is_5xx_server_error(status_code):
184+
error_code, error_context = _create_error_context(request, exception)
185+
186+
_logger.exception(
187+
**create_troubleshotting_log_kwargs(
188+
user_error_msg,
189+
error=exception,
190+
error_context=error_context,
191+
error_code=error_code,
192+
)
193+
)
194+
195+
return create_http_error(exception, user_error_msg, http_error_cls)
181196

182197

183198
def error_middleware_factory(api_version: str) -> Middleware:

0 commit comments

Comments
 (0)