Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Oct 14, 2024
1 parent 70969b0 commit 58df82a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from models_library.errors_classes import OsparcErrorMixin
from models_library.utils.json_serialization import json_dumps

from ..logging_utils import create_troubleshotting_log_message, get_log_record_extra
from ..logging_errors import create_troubleshotting_log_message, get_log_record_extra
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
from ..utils import is_production_environ
from .rest_models import ErrorItemType, ErrorType, LogMessageType
Expand Down
68 changes: 68 additions & 0 deletions packages/service-library/src/servicelib/logging_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
from pprint import pformat
from typing import Any

from models_library.error_codes import ErrorCodeStr, create_error_code
from models_library.errors_classes import OsparcErrorMixin

from .logging_errors import get_log_record_extra

_logger = logging.getLogger(__name__)


def create_troubleshotting_log_message(
message_to_user: str,
error: BaseException,
error_code: ErrorCodeStr,
error_context: dict[str, Any] | None = None,
tip: str | None = None,
) -> str:
"""Create a formatted message for _logger.exception(...)
Arguments:
message_to_user -- A user-friendly message to be displayed on the front-end explaining the issue in simple terms.
error -- the instance of the handled exception
error_code -- A unique error code (e.g., OEC or osparc-specific) to identify the type or source of the error for easier tracking.
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)
tip -- Helpful suggestions or possible solutions explaining why the error may have occurred and how it could potentially be resolved
"""
debug_data = pformat(
{
"exception_details": f"{error}",
"error_code": error_code,
"context": pformat(error_context, indent=1),
"tip": tip,
},
indent=1,
)

return f"{message_to_user}.\n{debug_data}"


def create_troubleshotting_log_kwargs(
message_to_user: str,
exception: BaseException,
error_context: dict[str, Any] | None = None,
tip: str | None = None,
):
error_code = create_error_code(exception)

context = error_context or {}
if isinstance(exception, OsparcErrorMixin):
context.update(exception.error_context())

log_msg = create_troubleshotting_log_message(
message_to_user=message_to_user,
error=exception,
error_code=error_code,
error_context=context,
tip=tip,
)

return {
"msg": log_msg,
"extra": get_log_record_extra(
error_code=error_code,
user_id=context.get("user_id", None),
),
}
32 changes: 0 additions & 32 deletions packages/service-library/src/servicelib/logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
from pathlib import Path
from typing import Any, TypeAlias, TypedDict, TypeVar

from models_library.error_codes import ErrorCodeStr
from models_library.utils.json_serialization import json_dumps

from .utils_secrets import mask_sensitive_data

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -346,35 +343,6 @@ def get_log_record_extra(
return extra or None


def create_troubleshotting_log_message(
message_to_user: str,
error: BaseException,
error_code: ErrorCodeStr,
error_context: dict[str, Any] | None = None,
tip: str | None = None,
) -> str:
"""Create a formatted message for _logger.exception(...)
Arguments:
message_to_user -- A user-friendly message to be displayed on the front-end explaining the issue in simple terms.
error -- the instance of the handled exception
error_code -- A unique error code (e.g., OEC or osparc-specific) to identify the type or source of the error for easier tracking.
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)
tip -- Helpful suggestions or possible solutions explaining why the error may have occurred and how it could potentially be resolved
"""
debug_data = json_dumps(
{
"exception_details": f"{error}",
"error_code": error_code,
"context": error_context,
"tip": tip,
},
indent=1,
)

return f"{message_to_user}.\n{debug_data}"


def _un_capitalize(s: str) -> str:
return s[:1].lower() + s[1:] if s else ""

Expand Down
48 changes: 48 additions & 0 deletions packages/service-library/tests/test_logging_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# pylint:disable=redefined-outer-name

import logging

import pytest
from models_library.error_codes import create_error_code
from models_library.errors_classes import OsparcErrorMixin
from servicelib.logging_errors import create_troubleshotting_log_message
from servicelib.logging_utils import get_log_record_extra


def test_create_troubleshotting_log_message(caplog: pytest.LogCaptureFixture):
class MyError(OsparcErrorMixin, RuntimeError):
msg_template = "My error {user_id}"

with pytest.raises(MyError) as exc_info:
raise MyError(user_id=123, product_name="foo")

exc = exc_info.value
error_code = create_error_code(exc)
log_msg = create_troubleshotting_log_message(
f"Nice message to user [{error_code}]",
exc,
error_code=error_code,
error_context=exc.error_context(),
tip="This is a test error",
)

with caplog.at_level(logging.WARNING):
root_logger = logging.getLogger()
root_logger.exception(
log_msg, extra=get_log_record_extra(error_code=error_code)
)

# ERROR root:test_logging_utils.py:417 Nice message to user [OEC:126055703573984].
# {
# "exception_details": "My error 123",
# "error_code": "OEC:126055703573984",
# "context": {
# "user_id": 123,
# "product_name": "foo"
# },
# "tip": "This is a test error"
# }

assert error_code in caplog.text
assert "user_id" in caplog.text
assert "product_name" in caplog.text
43 changes: 0 additions & 43 deletions packages/service-library/tests/test_logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

import pytest
from faker import Faker
from models_library.error_codes import create_error_code
from models_library.errors_classes import OsparcErrorMixin
from servicelib.logging_utils import (
LogExtra,
LogLevelInt,
LogMessageStr,
create_troubleshotting_log_message,
get_log_record_extra,
guess_message_log_level,
log_context,
log_decorator,
Expand Down Expand Up @@ -381,42 +377,3 @@ def test_set_parent_module_log_level_(caplog: pytest.LogCaptureFixture):

assert "parent warning" in caplog.text
assert "child warning" in caplog.text


def test_create_troubleshotting_log_message(caplog: pytest.LogCaptureFixture):
class MyError(OsparcErrorMixin, RuntimeError):
msg_template = "My error {user_id}"

with pytest.raises(MyError) as exc_info:
raise MyError(user_id=123, product_name="foo")

exc = exc_info.value
error_code = create_error_code(exc)
log_msg = create_troubleshotting_log_message(
f"Nice message to user [{error_code}]",
exc,
error_code=error_code,
error_context=exc.error_context(),
tip="This is a test error",
)

with caplog.at_level(logging.WARNING):
root_logger = logging.getLogger()
root_logger.exception(
log_msg, extra=get_log_record_extra(error_code=error_code)
)

# ERROR root:test_logging_utils.py:417 Nice message to user [OEC:126055703573984].
# {
# "exception_details": "My error 123",
# "error_code": "OEC:126055703573984",
# "context": {
# "user_id": 123,
# "product_name": "foo"
# },
# "tip": "This is a test error"
# }

assert error_code in caplog.text
assert "user_id" in caplog.text
assert "product_name" in caplog.text
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
parse_request_query_parameters_as,
)
from servicelib.aiohttp.typing_extension import Handler
from servicelib.logging_utils import (
create_troubleshotting_log_message,
get_log_record_extra,
)
from servicelib.logging_errors import create_troubleshotting_log_message
from servicelib.logging_utils import get_log_record_extra
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
from servicelib.request_keys import RQT_USERID_KEY
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
Expand Down

0 comments on commit 58df82a

Please sign in to comment.