10000 feat: Add more details to MissingTargetException error (#189) · jrmfg/functions-framework-python@b7055ed · GitHub
[go: up one dir, main page]

Skip to content

Commit b7055ed

Browse files
authored
feat: Add more details to MissingTargetException error (GoogleCloudPlatform#189)
* 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>
1 parent b4ed666 commit b7055ed

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/functions_framework/_function_registry.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,21 @@ def get_user_function(source, source_module, target):
3838
"""Returns user function, raises exception for invalid function."""
3939
# Extract the target function from the source file
4040
if not hasattr(source_module, target):
41+
non_target_functions = ", ".join(
42+
"'{attr}'".format(attr=attr)
43+
for attr in dir(source_module)
44+
if isinstance(getattr(source_module, attr), types.FunctionType)
45+
)
4146
raise MissingTargetException(
42-
"File {source} is expected to contain a function named {target}".format(
43-
source=source, target=target
47+
"File {source} is expected to contain a function named '{target}'. Found: {non_target_functions} instead".format(
48+
source=source, target=target, non_target_functions=non_target_functions
4449
)
4550
)
4651
function = getattr(source_module, target)
4752
# Check that it is a function
4853
if not isinstance(function, types.FunctionType):
4954
raise InvalidTargetTypeException(
50-
"The function defined in file {source} as {target} needs to be of "
55+
"The function defined in file {source} as '{target}' needs to be of "
5156
"type function. Got: invalid type {target_type}".format(
5257
source=source, target=target, target_type=type(function)
5358
)

tests/test_functions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def test_invalid_function_definition_multiple_entry_points():
275275
create_app(target, source, "event")
276276

277277
assert re.match(
278-
"File .* is expected to contain a function named function", str(excinfo.value)
278+
"File .* is expected to contain a function named 'function'. Found: 'fun', 'myFunctionBar', 'myFunctionFoo' instead",
279+
str(excinfo.value),
279280
)
280281

281282

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

289290
assert re.match(
290-
"File .* is expected to contain a function named invalidFunction",
291+
"File .* is expected to contain a function named 'invalidFunction'. Found: 'fun', 'myFunctionBar', 'myFunctionFoo' instead",
291292
str(excinfo.value),
292293
)
293294

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

302303
assert re.match(
303-
"The function defined in file .* as notAFunction needs to be of type "
304+
"The function defined in file .* as 'notAFunction' needs to be of type "
304305
"function. Got: .*",
305306
str(excinfo.value),
306307
)

0 commit comments

Comments
 (0)
0