Skip to content

Commit

Permalink
relocate, update, and rename variable & lookup exceptions (#439)
Browse files Browse the repository at this point in the history
- variable and lookup exceptions are not top level
- renamed some exceptions for more clarity
- removed a redundant exception
- update some type hints
- update tests for new import locations
  • Loading branch information
ITProKyle committed Mar 22, 2021
1 parent d4ad802 commit dc334bb
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 331 deletions.
14 changes: 7 additions & 7 deletions runway/cfngin/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from ..exceptions import (
InvalidUserdataPlaceholder,
MissingVariable,
UnresolvedVariable,
UnresolvedVariables,
UnresolvedBlueprintVariable,
UnresolvedBlueprintVariables,
ValidatorError,
VariableTypeRequired,
)
Expand Down Expand Up @@ -188,8 +188,8 @@ def resolve_variable(var_name, var_def, provided_variable, blueprint_name):
Raises:
MissingVariable: Raised when a variable with no default is not
provided a value.
UnresolvedVariable: Raised when the provided variable is not already
resolved.
UnresolvedBlueprintVariable: Raised when the provided variable is
not already resolved.
ValueError: Raised when the value is not the right type and cannot be
cast as the correct type. Raised by
:func:`runway.cfngin.blueprints.base.validate_variable_type`
Expand All @@ -204,7 +204,7 @@ def resolve_variable(var_name, var_def, provided_variable, blueprint_name):

if provided_variable:
if not provided_variable.resolved:
raise UnresolvedVariable(blueprint_name, provided_variable)
raise UnresolvedBlueprintVariable(blueprint_name, provided_variable)

value = provided_variable.value
else:
Expand Down Expand Up @@ -425,11 +425,11 @@ def get_variables(self):
Dict[str, Variable]: Variables available to the template.
Raises:
UnresolvedVariables: If variables are unresolved.
UnresolvedBlueprintVariables: If variables are unresolved.
"""
if self.resolved_variables is None:
raise UnresolvedVariables(self.name)
raise UnresolvedBlueprintVariables(self.name)
return self.resolved_variables

def get_cfn_parameters(self):
Expand Down
8 changes: 4 additions & 4 deletions runway/cfngin/blueprints/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from jinja2 import Environment, FileSystemLoader

from ..exceptions import InvalidConfig, UnresolvedVariable
from ..exceptions import InvalidConfig, UnresolvedBlueprintVariable
from ..util import parse_cloudformation_template
from .base import Blueprint

Expand Down Expand Up @@ -67,14 +67,14 @@ def resolve_variable(provided_variable, blueprint_name):
object: The resolved variable string value.
Raises:
UnresolvedVariable: Raised when the provided variable is not already
resolved.
UnresolvedBlueprintVariable: Raised when the provided variable is
not already resolved.
"""
value = None
if provided_variable:
if not provided_variable.resolved:
raise UnresolvedVariable(blueprint_name, provided_variable)
raise UnresolvedBlueprintVariable(blueprint_name, provided_variable)

value = provided_variable.value

Expand Down
149 changes: 2 additions & 147 deletions runway/cfngin/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,6 @@ def __init__(self, change_set_id):
super().__init__(message)


class FailedLookup(Exception):
"""Intermediary Exception to be converted to FailedVariableLookup.
Should be caught by error handling and
:class:`runway.cfngin.exceptions.FailedVariableLookup` raised instead to
construct a propper error message.
"""

def __init__(self, lookup, error, *args, **kwargs):
"""Instantiate class.
Args:
lookup (:class:`runway.cfngin.variables.VariableValueLookup`):
Attempted lookup and resulted in an exception being raised.
error (Exception): The exception that was raised.
"""
self.lookup = lookup
self.error = error
super().__init__("Failed lookup", *args, **kwargs)


class FailedVariableLookup(Exception):
"""Lookup could not be resolved.
Raised when an exception is raised when trying to resolve a lookup.
"""

def __init__(self, variable_name, lookup, error, *args, **kwargs):
"""Instantiate class.
Args:
variable_name (str): Name of the variable that failed to be
resolved.
lookup (:class:`runway.cfngin.variables.VariableValueLookup`):
Attempted lookup and resulted in an exception being raised.
error (Exception): The exception that was raised.
"""
self.lookup = lookup
self.error = error
message = "Couldn't resolve lookup in variable `%s`, " % variable_name
message += "lookup: ${%s}: " % repr(lookup)
message += "(%s) %s" % (error.__class__, error)
super().__init__(message, *args, **kwargs)


class GraphError(Exception):
"""Raised when the graph is invalid (e.g. acyclic dependencies)."""

Expand Down Expand Up @@ -137,43 +88,6 @@ def __init__(self, msg):
super().__init__(self.message)


class InvalidLookupCombination(Exception):
"""Improper use of lookups to result in a non-string return value."""

def __init__(self, lookup, lookups, value, *args, **kwargs):
"""Instantiate class.
Args:
lookup (:class:`runway.cfngin.variables.VariableValueLookup`): The
variable lookup that was attempted but did not return a string.
lookups (:class:`runway.cfngin.variables.VariableValueConcatenation`):
The full variable concatenation the failing lookup is part of.
value (Any): The non-string value returned by lookup.
"""
message = (
'Lookup: "{}" has non-string return value, must be only lookup '
'present (not {}) in "{}"'
).format(str(lookup), len(lookups), value)
super().__init__(message, *args, **kwargs)


class InvalidLookupConcatenation(Exception):
"""Intermediary Exception to be converted to InvalidLookupCombination.
Should be caught by error handling and
:class:`runway.cfngin.exceptions.InvalidLookupCombination` raised instead
to construct a propper error message.
"""

def __init__(self, lookup, lookups, *args, **kwargs):
"""Instantiate class."""
self.lookup = lookup
self.lookups = lookups
super().__init__("", *args, **kwargs)


class InvalidUserdataPlaceholder(Exception):
"""Raised when a placeholder name in raw_user_data is not valid.
Expand Down Expand Up @@ -248,24 +162,6 @@ def __init__(self, blueprint_name, variable_name, *args, **kwargs):
super().__init__(message, *args, **kwargs)


class OutputDoesNotExist(Exception):
"""Raised when a specific stack output does not exist."""

def __init__(self, stack_name, output, *args, **kwargs):
"""Instantiate class.
Args:
stack_name (str): Name of the stack.
output (str): The output that does not exist.
"""
self.stack_name = stack_name
self.output = output

message = "Output %s does not exist on stack %s" % (output, stack_name)
super().__init__(message, *args, **kwargs)


class PipError(Exception):
"""Raised when pip returns a non-zero exit code."""

Expand Down Expand Up @@ -500,27 +396,7 @@ def __init__(self, stack_name, change_set_id, status, status_reason):
super().__init__(message)


class UnknownLookupType(Exception):
"""Lookup type provided does not match a registered lookup.
Example:
If a lookup of ``${<lookup_type> query}`` is used and ``<lookup_type>``
is not a registered lookup, this exception will be raised.
"""

def __init__(self, lookup_type, *args, **kwargs):
"""Instantiate class.
Args:
lookup_type (str): Lookup type that was used but not registered.
"""
message = 'Unknown lookup type: "{}"'.format(lookup_type)
super().__init__(message, *args, **kwargs)


class UnresolvedVariable(Exception):
class UnresolvedBlueprintVariable(Exception): # TODO rename for blueprints only
"""Raised when trying to use a variable before it has been resolved."""

def __init__(self, blueprint_name, variable, *args, **kwargs):
Expand All @@ -539,28 +415,7 @@ def __init__(self, blueprint_name, variable, *args, **kwargs):
super().__init__(message, *args, **kwargs)


class UnresolvedVariableValue(Exception):
"""Intermediary Exception to be converted to UnresolvedVariable.
Should be caught by error handling and
:class:`runway.cfngin.exceptions.UnresolvedVariable` raised instead to
construct a propper error message.
"""

def __init__(self, lookup, *args, **kwargs):
"""Instantiate class.
Args:
lookup (:class:`runway.cfngin.variables.VariableValueLookup`): The
lookup that is not resolved.
"""
self.lookup = lookup
super().__init__("Unresolved lookup", *args, **kwargs)


class UnresolvedVariables(Exception):
class UnresolvedBlueprintVariables(Exception): # TODO rename for blueprints only
"""Raised when trying to use variables before they has been resolved."""

def __init__(self, blueprint_name, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion runway/cfngin/hooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from runway.util import load_object_from_string
from runway.variables import Variable, resolve_variables

from ...exceptions import FailedVariableLookup
from ..blueprints.base import Blueprint
from ..exceptions import FailedVariableLookup

LOGGER = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion runway/cfngin/lookups/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from runway.lookups.handlers import cfn, ecr, ssm
from runway.util import DOC_SITE, load_object_from_string

from ..exceptions import FailedVariableLookup, UnknownLookupType
from ...exceptions import FailedVariableLookup, UnknownLookupType
from .handlers import ami, default, dynamodb, envvar
from .handlers import file as file_handler
from .handlers import hook_data, kms, output, rxref, split, ssmstore, xref
Expand Down
2 changes: 1 addition & 1 deletion runway/core/components/_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from ..._logging import PrefixAdaptor
from ...cfngin.exceptions import UnresolvedVariable
from ...config import FutureDefinition, VariablesDefinition
from ...exceptions import UnresolvedVariable
from ...util import cached_property, merge_dicts, merge_nested_environment_dicts
from ..providers import aws
from ._module import Module
Expand Down
Loading

0 comments on commit dc334bb

Please sign in to comment.