Skip to content

Commit

Permalink
fix: Raise UserException when an invalid runtime is specified (#6701)
Browse files Browse the repository at this point in the history
* Raise a user exception instead of default Python exception when invalid runtime is used

* Commit the exception file

* make format
  • Loading branch information
lucashuy authored Feb 16, 2024
1 parent 780fd82 commit 0391162
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions samcli/local/docker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ class ContainerFailureError(UserException):
"""
Raised when the invoke container fails execution
"""


class InvalidRuntimeException(UserException):
"""
Raised when an invalid runtime is specified for a Lambda Function
"""
4 changes: 3 additions & 1 deletion samcli/local/docker/lambda_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List

from samcli.lib.utils.packagetype import IMAGE
from samcli.local.docker.exceptions import InvalidRuntimeException
from samcli.local.docker.lambda_debug_settings import LambdaDebugSettings

from .container import DEFAULT_CONTAINER_HOST_INTERFACE, Container
Expand All @@ -15,6 +16,7 @@
LOG = logging.getLogger(__name__)

RIE_LOG_LEVEL_ENV_VAR = "SAM_CLI_RIE_DEV"
INVALID_RUNTIME_MESSAGE = "Unsupported Lambda runtime: {runtime}. For a list of supported runtimes, please visit https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html"


class LambdaContainer(Container):
Expand Down Expand Up @@ -95,7 +97,7 @@ def __init__(
Optional. The function full path, unique in all stacks
"""
if not Runtime.has_value(runtime) and not packagetype == IMAGE:
raise ValueError("Unsupported Lambda runtime {}".format(runtime))
raise InvalidRuntimeException(INVALID_RUNTIME_MESSAGE.format(runtime=runtime))

image = LambdaContainer._get_image(
lambda_image, runtime, packagetype, imageuri, layers, architecture, function_full_path
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/local/docker/test_lambda_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from samcli.commands.local.lib.debug_context import DebugContext
from samcli.lib.utils.packagetype import IMAGE, ZIP
from samcli.local.docker.exceptions import InvalidRuntimeException
from samcli.local.docker.lambda_container import LambdaContainer, Runtime, RIE_LOG_LEVEL_ENV_VAR
from samcli.local.docker.lambda_debug_settings import DebuggingNotSupported
from samcli.local.docker.lambda_image import RAPID_IMAGE_TAG_PREFIX
Expand Down Expand Up @@ -433,7 +434,7 @@ def test_must_fail_for_unsupported_runtime(self):

image_builder_mock = Mock()

with self.assertRaises(ValueError) as context:
with self.assertRaises(InvalidRuntimeException) as context:
LambdaContainer(
runtime=runtime,
imageuri=self.imageuri,
Expand All @@ -446,7 +447,10 @@ def test_must_fail_for_unsupported_runtime(self):
architecture="x86_64",
)

self.assertEqual(str(context.exception), "Unsupported Lambda runtime foo")
self.assertEqual(
str(context.exception),
"Unsupported Lambda runtime: foo. For a list of supported runtimes, please visit https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html",
)

@parameterized.expand(
[
Expand Down

0 comments on commit 0391162

Please sign in to comment.