Skip to content

Commit a844f98

Browse files
committed
logs 500
1 parent c4c8628 commit a844f98

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from common_library.json_serialization import json_dumps, json_loads
1616
from common_library.user_messages import user_message
1717
from models_library.rest_error import ErrorGet, ErrorItemType, LogMessageType
18+
from servicelib.status_codes_utils import is_5xx_server_error
1819

1920
from ..logging_errors import create_troubleshotting_log_kwargs
2021
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
@@ -98,19 +99,36 @@ def _handle_http_error(
9899
if not exception.text or not is_enveloped_from_text(exception.text):
99100
# NOTE: aiohttp.HTTPException creates `text = f"{self.status}: {self.reason}"`
100101
# We do not like for the user to pop up a message like "401: You are not authorized"
101-
error_message = exception.reason or exception.text or "Unexpected error"
102+
user_error_msg = exception.reason or exception.text or "Unexpected error"
102103
error_model = ErrorGet(
103104
errors=[
104105
ErrorItemType.from_error(exception),
105106
],
106107
status=exception.status,
107108
logs=[
108-
LogMessageType(message=error_message, level="ERROR"),
109+
LogMessageType(message=user_error_msg, level="ERROR"),
109110
],
110-
message=error_message,
111+
message=user_error_msg,
111112
)
112113
exception.text = EnvelopeFactory(error=error_model).as_text()
113114

115+
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+
}
122+
123+
_logger.exception(
124+
**create_troubleshotting_log_kwargs(
125+
user_error_msg,
126+
error=exception,
127+
error_context=error_context,
128+
error_code=error_code,
129+
)
130+
)
131+
114132
return exception
115133

116134

0 commit comments

Comments
 (0)