Skip to content

Commit 58df82a

Browse files
committed
tests
1 parent 70969b0 commit 58df82a

File tree

6 files changed

+119
-80
lines changed

6 files changed

+119
-80
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from models_library.errors_classes import OsparcErrorMixin
1717
from models_library.utils.json_serialization import json_dumps
1818

19-
from ..logging_utils import create_troubleshotting_log_message, get_log_record_extra
19+
from ..logging_errors import create_troubleshotting_log_message, get_log_record_extra
2020
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
2121
from ..utils import is_production_environ
2222
from .rest_models import ErrorItemType, ErrorType, LogMessageType
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import logging
2+
from pprint import pformat
3+
from typing import Any
4+
5+
from models_library.error_codes import ErrorCodeStr, create_error_code
6+
from models_library.errors_classes import OsparcErrorMixin
7+
8+
from .logging_errors import get_log_record_extra
9+
10+
_logger = logging.getLogger(__name__)
11+
12+
13+
def create_troubleshotting_log_message(
14+
message_to_user: str,
15+
error: BaseException,
16+
error_code: ErrorCodeStr,
17+
error_context: dict[str, Any] | None = None,
18+
tip: str | None = None,
19+
) -> str:
20+
"""Create a formatted message for _logger.exception(...)
21+
22+
Arguments:
23+
message_to_user -- A user-friendly message to be displayed on the front-end explaining the issue in simple terms.
24+
error -- the instance of the handled exception
25+
error_code -- A unique error code (e.g., OEC or osparc-specific) to identify the type or source of the error for easier tracking.
26+
error_context -- Additional context surrounding the exception, such as environment variables or function-specific data. This can be derived from exc.error_context() (relevant when using the OsparcErrorMixin)
27+
tip -- Helpful suggestions or possible solutions explaining why the error may have occurred and how it could potentially be resolved
28+
"""
29+
debug_data = pformat(
30+
{
31+
"exception_details": f"{error}",
32+
"error_code": error_code,
33+
"context": pformat(error_context, indent=1),
34+
"tip": tip,
35+
},
36+
indent=1,
37+
)
38+
39+
return f"{message_to_user}.\n{debug_data}"
40+
41+
42+
def create_troubleshotting_log_kwargs(
43+
message_to_user: str,
44+
exception: BaseException,
45+
error_context: dict[str, Any] | None = None,
46+
tip: str | None = None,
47+
):
48+
error_code = create_error_code(exception)
49+
50+
context = error_context or {}
51+
if isinstance(exception, OsparcErrorMixin):
52+
context.update(exception.error_context())
53+
54+
log_msg = create_troubleshotting_log_message(
55+
message_to_user=message_to_user,
56+
error=exception,
57+
error_code=error_code,
58+
error_context=context,
59+
tip=tip,
60+
)
61+
62+
return {
63+
"msg": log_msg,
64+
"extra": get_log_record_extra(
65+
error_code=error_code,
66+
user_id=context.get("user_id", None),
67+
),
68+
}

packages/service-library/src/servicelib/logging_utils.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
from pathlib import Path
1717
from typing import Any, TypeAlias, TypedDict, TypeVar
1818

19-
from models_library.error_codes import ErrorCodeStr
20-
from models_library.utils.json_serialization import json_dumps
21-
2219
from .utils_secrets import mask_sensitive_data
2320

2421
_logger = logging.getLogger(__name__)
@@ -346,35 +343,6 @@ def get_log_record_extra(
346343
return extra or None
347344

348345

349-
def create_troubleshotting_log_message(
350-
message_to_user: str,
351-
error: BaseException,
352-
error_code: ErrorCodeStr,
353-
error_context: dict[str, Any] | None = None,
354-
tip: str | None = None,
355-
) -> str:
356-
"""Create a formatted message for _logger.exception(...)
357-
358-
Arguments:
359-
message_to_user -- A user-friendly message to be displayed on the front-end explaining the issue in simple terms.
360-
error -- the instance of the handled exception
361-
error_code -- A unique error code (e.g., OEC or osparc-specific) to identify the type or source of the error for easier tracking.
362-
error_context -- Additional context surrounding the exception, such as environment variables or function-specific data. This can be derived from exc.error_context() (relevant when using the OsparcErrorMixin)
363-
tip -- Helpful suggestions or possible solutions explaining why the error may have occurred and how it could potentially be resolved
364-
"""
365-
debug_data = json_dumps(
366-
{
367-
"exception_details": f"{error}",
368-
"error_code": error_code,
369-
"context": error_context,
370-
"tip": tip,
371-
},
372-
indent=1,
373-
)
374-
375-
return f"{message_to_user}.\n{debug_data}"
376-
377-
378346
def _un_capitalize(s: str) -> str:
379347
return s[:1].lower() + s[1:] if s else ""
380348

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# pylint:disable=redefined-outer-name
2+
3+
import logging
4+
5+
import pytest
6+
from models_library.error_codes import create_error_code
7+
from models_library.errors_classes import OsparcErrorMixin
8+
from servicelib.logging_errors import create_troubleshotting_log_message
9+
from servicelib.logging_utils import get_log_record_extra
10+
11+
12+
def test_create_troubleshotting_log_message(caplog: pytest.LogCaptureFixture):
13+
class MyError(OsparcErrorMixin, RuntimeError):
14+
msg_template = "My error {user_id}"
15+
16+
with pytest.raises(MyError) as exc_info:
17+
raise MyError(user_id=123, product_name="foo")
18+
19+
exc = exc_info.value
20+
error_code = create_error_code(exc)
21+
log_msg = create_troubleshotting_log_message(
22+
f"Nice message to user [{error_code}]",
23+
exc,
24+
error_code=error_code,
25+
error_context=exc.error_context(),
26+
tip="This is a test error",
27+
)
28+
29+
with caplog.at_level(logging.WARNING):
30+
root_logger = logging.getLogger()
31+
root_logger.exception(
32+
log_msg, extra=get_log_record_extra(error_code=error_code)
33+
)
34+
35+
# ERROR root:test_logging_utils.py:417 Nice message to user [OEC:126055703573984].
36+
# {
37+
# "exception_details": "My error 123",
38+
# "error_code": "OEC:126055703573984",
39+
# "context": {
40+
# "user_id": 123,
41+
# "product_name": "foo"
42+
# },
43+
# "tip": "This is a test error"
44+
# }
45+
46+
assert error_code in caplog.text
47+
assert "user_id" in caplog.text
48+
assert "product_name" in caplog.text

packages/service-library/tests/test_logging_utils.py

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

77
import pytest
88
from faker import Faker
9-
from models_library.error_codes import create_error_code
10-
from models_library.errors_classes import OsparcErrorMixin
119
from servicelib.logging_utils import (
1210
LogExtra,
1311
LogLevelInt,
1412
LogMessageStr,
15-
create_troubleshotting_log_message,
16-
get_log_record_extra,
1713
guess_message_log_level,
1814
log_context,
1915
log_decorator,
@@ -381,42 +377,3 @@ def test_set_parent_module_log_level_(caplog: pytest.LogCaptureFixture):
381377

382378
assert "parent warning" in caplog.text
383379
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/users/_handlers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
parse_request_query_parameters_as,
1111
)
1212
from servicelib.aiohttp.typing_extension import Handler
13-
from servicelib.logging_utils import (
14-
create_troubleshotting_log_message,
15-
get_log_record_extra,
16-
)
13+
from servicelib.logging_errors import create_troubleshotting_log_message
14+
from servicelib.logging_utils import get_log_record_extra
1715
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
1816
from servicelib.request_keys import RQT_USERID_KEY
1917
from servicelib.rest_constants import RESPONSE_MODEL_POLICY

0 commit comments

Comments
 (0)