-
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.
🎨 Enhanced error handling and troubleshooting logs helpers (#6531)
- Loading branch information
Showing
27 changed files
with
456 additions
and
277 deletions.
There are no files selected for viewing
File renamed without changes.
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
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,94 @@ | ||
import logging | ||
from pprint import pformat | ||
from typing import Any, TypedDict | ||
|
||
from models_library.error_codes import ErrorCodeStr | ||
from models_library.errors_classes import OsparcErrorMixin | ||
|
||
from .logging_utils import LogExtra, get_log_record_extra | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_troubleshotting_log_message( | ||
user_error_msg: str, | ||
*, | ||
error: BaseException, | ||
error_code: ErrorCodeStr | None = None, | ||
error_context: dict[str, Any] | None = None, | ||
tip: str | None = None, | ||
) -> str: | ||
"""Create a formatted message for _logger.exception(...) | ||
Arguments: | ||
user_error_msg -- 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_type": f"{type(error)}", | ||
"exception_details": f"{error}", | ||
"error_code": error_code, | ||
"context": pformat(error_context, indent=1), | ||
"tip": tip, | ||
}, | ||
indent=1, | ||
) | ||
|
||
return f"{user_error_msg}.\n{debug_data}" | ||
|
||
|
||
class LogKwargs(TypedDict): | ||
msg: str | ||
extra: LogExtra | None | ||
|
||
|
||
def create_troubleshotting_log_kwargs( | ||
user_error_msg: str, | ||
*, | ||
error: BaseException, | ||
error_code: ErrorCodeStr | None = None, | ||
error_context: dict[str, Any] | None = None, | ||
tip: str | None = None, | ||
) -> LogKwargs: | ||
""" | ||
Creates a dictionary of logging arguments to be used with _log.exception for troubleshooting purposes. | ||
Usage: | ||
try: | ||
... | ||
except MyException as exc | ||
_logger.exception( | ||
**create_troubleshotting_log_kwargs( | ||
user_error_msg=frontend_msg, | ||
exception=exc, | ||
tip="Check row in `groups_extra_properties` for this product. It might be missing.", | ||
) | ||
) | ||
""" | ||
# error-context | ||
context = error_context or {} | ||
if isinstance(error, OsparcErrorMixin): | ||
context.update(error.error_context()) | ||
|
||
# compose as log message | ||
log_msg = create_troubleshotting_log_message( | ||
user_error_msg, | ||
error=error, | ||
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
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 @@ | ||
# 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_kwargs, | ||
create_troubleshotting_log_message, | ||
) | ||
|
||
|
||
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) | ||
|
||
assert exc.error_code() == error_code | ||
|
||
msg = f"Nice message to user [{error_code}]" | ||
|
||
log_msg = create_troubleshotting_log_message( | ||
msg, | ||
error=exc, | ||
error_code=error_code, | ||
error_context=exc.error_context(), | ||
tip="This is a test error", | ||
) | ||
|
||
log_kwargs = create_troubleshotting_log_kwargs( | ||
msg, | ||
error=exc, | ||
error_code=error_code, | ||
tip="This is a test error", | ||
) | ||
|
||
assert log_kwargs["msg"] == log_msg | ||
assert log_kwargs["extra"] is not None | ||
assert ( | ||
# pylint: disable=unsubscriptable-object | ||
log_kwargs["extra"]["log_uid"] | ||
== "123" | ||
), "user_id is injected as extra from context" | ||
|
||
with caplog.at_level(logging.WARNING): | ||
root_logger = logging.getLogger() | ||
root_logger.exception(**log_kwargs) | ||
|
||
# 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
Oops, something went wrong.