|
15 | 15 | from common_library.json_serialization import json_dumps, json_loads |
16 | 16 | from common_library.user_messages import user_message |
17 | 17 | from models_library.rest_error import ErrorGet, ErrorItemType, LogMessageType |
| 18 | +from servicelib.status_codes_utils import is_5xx_server_error |
18 | 19 |
|
19 | 20 | from ..logging_errors import create_troubleshotting_log_kwargs |
20 | 21 | from ..mimetype_constants import MIMETYPE_APPLICATION_JSON |
@@ -98,19 +99,36 @@ def _handle_http_error( |
98 | 99 | if not exception.text or not is_enveloped_from_text(exception.text): |
99 | 100 | # NOTE: aiohttp.HTTPException creates `text = f"{self.status}: {self.reason}"` |
100 | 101 | # 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" |
102 | 103 | error_model = ErrorGet( |
103 | 104 | errors=[ |
104 | 105 | ErrorItemType.from_error(exception), |
105 | 106 | ], |
106 | 107 | status=exception.status, |
107 | 108 | logs=[ |
108 | | - LogMessageType(message=error_message, level="ERROR"), |
| 109 | + LogMessageType(message=user_error_msg, level="ERROR"), |
109 | 110 | ], |
110 | | - message=error_message, |
| 111 | + message=user_error_msg, |
111 | 112 | ) |
112 | 113 | exception.text = EnvelopeFactory(error=error_model).as_text() |
113 | 114 |
|
| 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 | + |
114 | 132 | return exception |
115 | 133 |
|
116 | 134 |
|
|
0 commit comments