Skip to content

Commit

Permalink
error
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Sep 30, 2024
1 parent 64f59e5 commit 559b5df
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
43 changes: 43 additions & 0 deletions packages/service-library/tests/test_logging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

import pytest
from faker import Faker
from models_library.errors_classes import OsparcErrorMixin
from servicelib.error_codes import create_error_code
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 @@ -377,3 +381,42 @@ 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
13 changes: 0 additions & 13 deletions services/web/server/src/simcore_service_webserver/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@
] = "Under development. Use WEBSERVER_DEV_FEATURES_ENABLED=1 to enable current implementation"


FMSG_SERVER_EXCEPTION_LOG: Final[
# formatted message for _logger.exception(...)
# Use these keys as guidance to provide necessary information for a good error message log
#
# user_msg: message seem by front-end user (should include OEC)
# exc: handled exception
# ctx: exception context e.g. exc.ctx() (see OsparcErrorMixin)
# tip: tips on why this might have happened and or possible solution
#
str
] = "{user_msg}.\nERROR: {exc}.\nCONTEXT: {ctx}.\nTIP: {tip}\n"


__all__: tuple[str, ...] = (
"APP_CONFIG_KEY",
"APP_AIOPG_ENGINE_KEY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
)
from servicelib.aiohttp.typing_extension import Handler
from servicelib.error_codes import create_error_code
from servicelib.logging_utils import (
create_troubleshotting_log_message,
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

from .._constants import FMSG_SERVER_EXCEPTION_LOG, RQ_PRODUCT_KEY
from .._constants import RQ_PRODUCT_KEY
from .._meta import API_VTAG
from ..login.decorators import login_required
from ..security.decorators import permission_required
Expand Down Expand Up @@ -50,18 +54,23 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
raise web.HTTPNotFound(reason=f"{exc}") from exc
except MissingGroupExtraPropertiesForProductError as exc:
error_code = create_error_code(exc)
user_msg = FMSG_MISSING_CONFIG_WITH_OEC.format(error_code)
log_msg = FMSG_SERVER_EXCEPTION_LOG.format(
user_msg=user_msg,
exc=exc,
ctx=exc.ctx(),
frontend_msg = FMSG_MISSING_CONFIG_WITH_OEC.format(error_code)
log_msg = create_troubleshotting_log_message(
message_to_user=frontend_msg,
error=exc,
error_code=error_code,
error_context=exc.error_context(),
tip="Row in `groups_extra_properties` for this product is missing.",
)

_logger.exception(
log_msg,
extra={"error_code": error_code},
extra=get_log_record_extra(
error_code=error_code,
user_id=exc.error_context().get("user_id", None),
),
)
raise web.HTTPServiceUnavailable(reason=user_msg) from exc
raise web.HTTPServiceUnavailable(reason=frontend_msg) from exc

return wrapper

Expand Down

0 comments on commit 559b5df

Please sign in to comment.