Skip to content

Commit 559b5df

Browse files
committed
error
1 parent 64f59e5 commit 559b5df

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

packages/service-library/tests/test_logging_utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
import pytest
88
from faker import Faker
9+
from models_library.errors_classes import OsparcErrorMixin
10+
from servicelib.error_codes import create_error_code
911
from servicelib.logging_utils import (
1012
LogExtra,
1113
LogLevelInt,
1214
LogMessageStr,
15+
create_troubleshotting_log_message,
16+
get_log_record_extra,
1317
guess_message_log_level,
1418
log_context,
1519
log_decorator,
@@ -377,3 +381,42 @@ def test_set_parent_module_log_level_(caplog: pytest.LogCaptureFixture):
377381

378382
assert "parent warning" in caplog.text
379383
assert "child warning" in caplog.text
384+
385+
386+
def test_create_troubleshotting_log_message(caplog: pytest.LogCaptureFixture):
387+
class MyError(OsparcErrorMixin, RuntimeError):
388+
msg_template = "My error {user_id}"
389+
390+
with pytest.raises(MyError) as exc_info:
391+
raise MyError(user_id=123, product_name="foo")
392+
393+
exc = exc_info.value
394+
error_code = create_error_code(exc)
395+
log_msg = create_troubleshotting_log_message(
396+
f"Nice message to user [{error_code}]",
397+
exc,
398+
error_code=error_code,
399+
error_context=exc.error_context(),
400+
tip="This is a test error",
401+
)
402+
403+
with caplog.at_level(logging.WARNING):
404+
root_logger = logging.getLogger()
405+
root_logger.exception(
406+
log_msg, extra=get_log_record_extra(error_code=error_code)
407+
)
408+
409+
# ERROR root:test_logging_utils.py:417 Nice message to user [OEC:126055703573984].
410+
# {
411+
# "exception_details": "My error 123",
412+
# "error_code": "OEC:126055703573984",
413+
# "context": {
414+
# "user_id": 123,
415+
# "product_name": "foo"
416+
# },
417+
# "tip": "This is a test error"
418+
# }
419+
420+
assert error_code in caplog.text
421+
assert "user_id" in caplog.text
422+
assert "product_name" in caplog.text

services/web/server/src/simcore_service_webserver/_constants.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@
2828
] = "Under development. Use WEBSERVER_DEV_FEATURES_ENABLED=1 to enable current implementation"
2929

3030

31-
FMSG_SERVER_EXCEPTION_LOG: Final[
32-
# formatted message for _logger.exception(...)
33-
# Use these keys as guidance to provide necessary information for a good error message log
34-
#
35-
# user_msg: message seem by front-end user (should include OEC)
36-
# exc: handled exception
37-
# ctx: exception context e.g. exc.ctx() (see OsparcErrorMixin)
38-
# tip: tips on why this might have happened and or possible solution
39-
#
40-
str
41-
] = "{user_msg}.\nERROR: {exc}.\nCONTEXT: {ctx}.\nTIP: {tip}\n"
42-
43-
4431
__all__: tuple[str, ...] = (
4532
"APP_CONFIG_KEY",
4633
"APP_AIOPG_ENGINE_KEY",

services/web/server/src/simcore_service_webserver/users/_handlers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
)
1111
from servicelib.aiohttp.typing_extension import Handler
1212
from servicelib.error_codes import create_error_code
13+
from servicelib.logging_utils import (
14+
create_troubleshotting_log_message,
15+
get_log_record_extra,
16+
)
1317
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
1418
from servicelib.request_keys import RQT_USERID_KEY
1519
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
1620

17-
from .._constants import FMSG_SERVER_EXCEPTION_LOG, RQ_PRODUCT_KEY
21+
from .._constants import RQ_PRODUCT_KEY
1822
from .._meta import API_VTAG
1923
from ..login.decorators import login_required
2024
from ..security.decorators import permission_required
@@ -50,18 +54,23 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
5054
raise web.HTTPNotFound(reason=f"{exc}") from exc
5155
except MissingGroupExtraPropertiesForProductError as exc:
5256
error_code = create_error_code(exc)
53-
user_msg = FMSG_MISSING_CONFIG_WITH_OEC.format(error_code)
54-
log_msg = FMSG_SERVER_EXCEPTION_LOG.format(
55-
user_msg=user_msg,
56-
exc=exc,
57-
ctx=exc.ctx(),
57+
frontend_msg = FMSG_MISSING_CONFIG_WITH_OEC.format(error_code)
58+
log_msg = create_troubleshotting_log_message(
59+
message_to_user=frontend_msg,
60+
error=exc,
61+
error_code=error_code,
62+
error_context=exc.error_context(),
5863
tip="Row in `groups_extra_properties` for this product is missing.",
5964
)
65+
6066
_logger.exception(
6167
log_msg,
62-
extra={"error_code": error_code},
68+
extra=get_log_record_extra(
69+
error_code=error_code,
70+
user_id=exc.error_context().get("user_id", None),
71+
),
6372
)
64-
raise web.HTTPServiceUnavailable(reason=user_msg) from exc
73+
raise web.HTTPServiceUnavailable(reason=frontend_msg) from exc
6574

6675
return wrapper
6776

0 commit comments

Comments
 (0)