Skip to content

Commit

Permalink
fix: override _run_inner to catch exceptions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sergiusens committed Sep 18, 2024
1 parent fbedf67 commit 1f4734f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} "
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,20 +665,20 @@ 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),
)

return_code = application.main()

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(),
)

Expand All @@ -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
"""
Expand Down

0 comments on commit 1f4734f

Please sign in to comment.