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 1bc9e1f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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
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 1bc9e1f

Please sign in to comment.