Skip to content

Commit

Permalink
bases: improve errors for ESM bases
Browse files Browse the repository at this point in the history
Bases that were once supported and no longer supported in the current
version provide a reference to the version/channel of Snapcraft where
they once were.

Signed-off-by: Sergio Schvezov <sergio.schvezov@canonical.com>
  • Loading branch information
sergiusens committed Jul 12, 2023
1 parent 4c6b1fd commit 3150ce2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
23 changes: 23 additions & 0 deletions snapcraft/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

"""Snapcraft error definitions."""

from typing import Optional

from craft_cli import CraftError


Expand Down Expand Up @@ -117,6 +119,27 @@ class LegacyFallback(Exception):
"""Fall back to legacy snapcraft implementation."""


class MaintenanceBase(SnapcraftError):
"""Error for bases under ESM and no longer supported in this release."""

def __init__(self, base) -> None:
channel: Optional[str] = None
if base == "core":
channel = "4.x"
elif base == "core18":
channel = "7.x"

resolution: Optional[str] = None
if channel:
resolution = f"Install from or refresh to the {channel!r} channel."

super().__init__(
f"{base!r} is not supported on this version of Snapcraft.",
resolution=resolution,
docs_url="https://snapcraft.io/docs/base-snaps",
)


class StoreCredentialsUnauthorizedError(SnapcraftError):
"""Error raised for 401 responses from the Snap Store."""

Expand Down
5 changes: 4 additions & 1 deletion snapcraft/parts/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

from snapcraft import errors, utils

_LEGACY_BASES = {"core", "core18", "core20"}
_ESM_BASES = {"core", "core18"}
_LEGACY_BASES = {"core20"}


def _check_duplicate_keys(node):
Expand Down Expand Up @@ -90,6 +91,8 @@ def load(filestream: TextIO) -> Dict[str, Any]:

if build_base is None:
raise errors.LegacyFallback("no base defined")
if build_base in _ESM_BASES:
raise errors.MaintenanceBase(build_base)
if build_base in _LEGACY_BASES:
raise errors.LegacyFallback(f"base is {build_base}")
except yaml.error.YAMLError as err:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/parts/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ def test_yaml_load_not_core22_base():
assert str(raised.value) == "base is core20"


def test_yaml_load_esm_base():
with pytest.raises(errors.MaintenanceBase):
yaml_utils.load(
io.StringIO(
dedent(
"""\
base: core
"""
)
)
)


def test_yaml_load_no_base():
with pytest.raises(errors.LegacyFallback) as raised:
yaml_utils.load(
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Snapcraft error tests."""

import pytest

from snapcraft import errors


@pytest.mark.parametrize("base,channel", [("core", "4.x"), ("core18", "7.x")])
def test_maintenance_base(base, channel):
message = f"{base!r} is not supported on this version of Snapcraft."
resolution = f"Install from or refresh to the {channel!r} channel."

error = errors.MaintenanceBase(base)

assert str(error) == message
assert error.resolution == resolution
assert error.docs_url == "https://snapcraft.io/docs/base-snaps"


def test_maintenance_base_fallback():
"""For when the base is not explicitly handled in the exception."""
error = errors.MaintenanceBase("unknown-base")

assert (
str(error) == f"'unknown-base' is not supported on this version of Snapcraft."
)
assert error.resolution == None
assert error.docs_url == "https://snapcraft.io/docs/base-snaps"

0 comments on commit 3150ce2

Please sign in to comment.