Skip to content

Commit

Permalink
feat: Add more details to MissingTargetException error (#189)
Browse files Browse the repository at this point in the history
* feat: more detailed function target error message

Signed-off-by: Grant Timmerman <744973+grant@users.noreply.github.com>

* feat: more detailed function target error message (2)

Signed-off-by: Grant Timmerman <744973+grant@users.noreply.github.com>

* ci: fix tests

Signed-off-by: Grant Timmerman <744973+grant@users.noreply.github.com>

* ci: fix ci

Signed-off-by: Grant Timmerman <744973+grant@users.noreply.github.com>
  • Loading branch information
grant authored Jun 9, 2022
1 parent b4ed666 commit b7055ed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/functions_framework/_function_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ def get_user_function(source, source_module, target):
"""Returns user function, raises exception for invalid function."""
# Extract the target function from the source file
if not hasattr(source_module, target):
non_target_functions = ", ".join(
"'{attr}'".format(attr=attr)
for attr in dir(source_module)
if isinstance(getattr(source_module, attr), types.FunctionType)
)
raise MissingTargetException(
"File {source} is expected to contain a function named {target}".format(
source=source, target=target
"File {source} is expected to contain a function named '{target}'. Found: {non_target_functions} instead".format(
source=source, target=target, non_target_functions=non_target_functions
)
)
function = getattr(source_module, target)
# Check that it is a function
if not isinstance(function, types.FunctionType):
raise InvalidTargetTypeException(
"The function defined in file {source} as {target} needs to be of "
"The function defined in file {source} as '{target}' needs to be of "
"type function. Got: invalid type {target_type}".format(
source=source, target=target, target_type=type(function)
)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ def test_invalid_function_definition_multiple_entry_points():
create_app(target, source, "event")

assert re.match(
"File .* is expected to contain a function named function", str(excinfo.value)
"File .* is expected to contain a function named 'function'. Found: 'fun', 'myFunctionBar', 'myFunctionFoo' instead",
str(excinfo.value),
)


Expand All @@ -287,7 +288,7 @@ def test_invalid_function_definition_multiple_entry_points_invalid_function():
create_app(target, source, "event")

assert re.match(
"File .* is expected to contain a function named invalidFunction",
"File .* is expected to contain a function named 'invalidFunction'. Found: 'fun', 'myFunctionBar', 'myFunctionFoo' instead",
str(excinfo.value),
)

Expand All @@ -300,7 +301,7 @@ def test_invalid_function_definition_multiple_entry_points_not_a_function():
create_app(target, source, "event")

assert re.match(
"The function defined in file .* as notAFunction needs to be of type "
"The function defined in file .* as 'notAFunction' needs to be of type "
"function. Got: .*",
str(excinfo.value),
)
Expand Down

0 comments on commit b7055ed

Please sign in to comment.