-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters