From 1f4734fb45719229e3aadce0be02f01c00f2fd8d Mon Sep 17 00:00:00 2001 From: Sergio Schvezov Date: Fri, 13 Sep 2024 10:23:45 -0300 Subject: [PATCH] fix: override _run_inner to catch exceptions Remove the heading from the original error to not give the impression it is a craft application error to the user, but instead an error generated store side. Signed-off-by: Sergio Schvezov --- snapcraft/application.py | 10 ++++------ tests/unit/test_application.py | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/snapcraft/application.py b/snapcraft/application.py index 706dd87b21..ee7229fa87 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -209,13 +209,13 @@ def _pre_run(self, dispatcher: craft_cli.Dispatcher) -> None: super()._pre_run(dispatcher) @override - def run(self) -> int: + def _run_inner(self) -> int: try: - return_code = super().run() + return_code = super()._run_inner() except craft_store.errors.NoKeyringError as err: self._emit_error( craft_cli.errors.CraftError( - f"craft-store error: {err}", + f"{err}", resolution=( "Ensure the keyring is working or " f"{store.constants.ENVIRONMENT_STORE_CREDENTIALS} " @@ -227,9 +227,7 @@ def run(self) -> int: return_code = 1 except craft_store.errors.CraftStoreError as err: self._emit_error( - craft_cli.errors.CraftError( - f"craft-store error: {err}", resolution=err.resolution - ), + craft_cli.errors.CraftError(f"{err}", resolution=err.resolution), cause=err, ) return_code = 1 diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 3f507b8663..e4ee1d5998 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -665,7 +665,7 @@ def test_known_core24(snapcraft_yaml, base, build_base, is_known_core24): ) def test_store_error(mocker, capsys, message, resolution, expected_message): mocker.patch( - "snapcraft.application.Application.run", + "snapcraft.application.Application._run_inner", side_effect=craft_store.errors.CraftStoreError(message, resolution=resolution), ) @@ -673,12 +673,12 @@ def test_store_error(mocker, capsys, message, resolution, expected_message): assert return_code == 1 _, err = capsys.readouterr() - assert f"craft-store error: {expected_message}" in err + assert expected_message in err def test_store_key_error(mocker, capsys): mocker.patch( - "snapcraft.application.Application.run", + "snapcraft.application.Application._run_inner", side_effect=craft_store.errors.NoKeyringError(), ) @@ -692,7 +692,7 @@ def test_store_key_error(mocker, capsys): # pylint: disable=[line-too-long] dedent( """\ - craft-store error: No keyring found to store or retrieve credentials from. + No keyring found to store or retrieve credentials from. Recommended resolution: Ensure the keyring is working or SNAPCRAFT_STORE_CREDENTIALS is correctly exported into the environment For more information, check out: https://snapcraft.io/docs/snapcraft-authentication """