8000 feat: Add more details to MissingTargetException error by grant · Pull Request #189 · GoogleCloudPlatform/functions-framework-python · GitHub
[go: up one dir, main page]

Skip to content

feat: Add more details to MissingTargetException error #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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