Skip to content

Commit

Permalink
feat: unxpected error messages point to correct log path if not root
Browse files Browse the repository at this point in the history
added util function to get proper log path based on if currently root or not
  • Loading branch information
a-dubs committed Dec 6, 2023
1 parent 9771152 commit 0b86695
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
5 changes: 4 additions & 1 deletion uaclient/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,10 @@ def wrapper(*args, **kwargs):
LOG.exception("Unhandled exception, please file a bug")
lock.clear_lock_file_if_present()
event.info(
info_msg=messages.UNEXPECTED_ERROR.format(error_msg=str(e)),
info_msg=messages.UNEXPECTED_ERROR.format(
error_msg=str(e),
log_path=util.get_user_or_root_log_path(),
),
file_type=sys.stderr,
)
event.error(
Expand Down
8 changes: 6 additions & 2 deletions uaclient/cli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mock
import pytest

from uaclient import defaults, exceptions, messages, status
from uaclient import defaults, exceptions, messages, status, util
from uaclient.cli import (
action_help,
assert_attached,
Expand Down Expand Up @@ -492,12 +492,15 @@ class TestMain:
(
TypeError("'NoneType' object is not subscriptable"),
messages.UNEXPECTED_ERROR.format(
error_msg="'NoneType' object is not subscriptable"
error_msg="'NoneType' object is not subscriptable",
log_path="fake_log_path",
),
"Unhandled exception, please file a bug",
),
),
)

@mock.patch("uaclient.util.get_user_or_root_log_path", return_value="fake_log_path")
@mock.patch(M_PATH_UACONFIG + "delete_cache_key")
@mock.patch("uaclient.cli.event.info")
@mock.patch("uaclient.cli.LOG.exception")
Expand All @@ -510,6 +513,7 @@ def test_errors_handled_gracefully(
m_log_exception,
m_event_info,
m_delete_cache_key,
_m_get_user_or_root_log_path,
event,
exception,
expected_error_msg,
Expand Down
7 changes: 6 additions & 1 deletion uaclient/cli/tests/test_cli_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,15 @@ def test_attach_config_enable_services(
),
(
Exception("error"),
messages.UNEXPECTED_ERROR.format(error_msg="error"),
messages.UNEXPECTED_ERROR.format(
error_msg="error",
log_path="fake_log_path",
),
messages.E_ATTACH_FAILURE_UNEXPECTED,
),
),
)
@mock.patch("uaclient.util.get_user_or_root_log_path", return_value="fake_log_path")
@mock.patch("uaclient.files.state_files.attachment_data_file.write")
@mock.patch("uaclient.entitlements.entitlements_enable_order")
@mock.patch("uaclient.contract.process_entitlement_delta")
Expand All @@ -534,6 +538,7 @@ def test_attach_when_one_service_fails_to_enable(
m_process_entitlement_delta,
m_enable_order,
_m_attachment_data_file_write,
_m_get_user_or_root_log_path,
expected_exception,
expected_msg,
expected_outer_msg,
Expand Down
5 changes: 4 additions & 1 deletion uaclient/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ def process_entitlements_delta(
failed_services=[
(
name,
messages.UNEXPECTED_ERROR.format(error_msg=str(exception)),
messages.UNEXPECTED_ERROR.format(
error_msg=str(exception),
log_path=util.get_user_or_root_log_path(),
),
)
for name, exception in zip(failed_services, unexpected_errors)
]
Expand Down
2 changes: 1 addition & 1 deletion uaclient/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ def __repr__(self):
t.gettext(
"""\
Unexpected error(s) occurred: {error_msg}
For more details, see the log: ~/.cache/ubuntu-pro/ubuntu-pro.log
For more details, see the log: {log_path}
First, try searching online for solutions to this error.
Otherwise, file a bug using the command: ubuntu-bug ubuntu-advantage-tools"""
),
Expand Down
7 changes: 7 additions & 0 deletions uaclient/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,10 @@ def print_package_list(
)
)
print("")


def get_user_or_root_log_path() -> str:
if we_are_currently_root():
return "/var/log/ubuntu-advantage.log"
else:
return "~/.cache/ubuntu-pro/ubuntu-pro.log"

0 comments on commit 0b86695

Please sign in to comment.