Skip to content

Commit

Permalink
fix: override _run_inner instead of run 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 13, 2024
1 parent 8faa6d5 commit daf8444
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 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
4 changes: 2 additions & 2 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,15 +665,15 @@ 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):
Expand Down

0 comments on commit daf8444

Please sign in to comment.