Skip to content
Closed
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
23 changes: 21 additions & 2 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,30 @@ def handle_none(rv):

# Execute the module, within the application context
with _app.app_context():
spec.loader.exec_module(source_module)
try:
spec.loader.exec_module(source_module)
function = _function_registry.get_user_function(
source, source_module, target
)
except Exception as e:
if werkzeug.serving.is_running_from_reloader():
# When reloading, print out the error immediately, but raise
# it later so the debugger or server can handle it.
import traceback

traceback.print_exc()
err = e

def function(*_args, **_kwargs):
raise err from None

else:
# When not reloading, raise the error immediately so the
# command fails.
raise e from None

# Get the configured function signature type
signature_type = _function_registry.get_func_signature_type(target, signature_type)
function = _function_registry.get_user_function(source, source_module, target)

_configure_app(_app, function, signature_type)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ def test_invalid_function_definition_function_syntax_error():
)


def test_invalid_function_definition_function_syntax_robustness_with_debug(monkeypatch):
monkeypatch.setattr(
functions_framework.werkzeug.serving, "is_running_from_reloader", lambda: True
)
source = TEST_FUNCTIONS_DIR / "background_load_error" / "main.py"
target = "function"

client = create_app(target, source).test_client()

resp = client.get("/")
assert resp.status_code == 500


def test_invalid_function_definition_missing_dependency():
source = TEST_FUNCTIONS_DIR / "background_missing_dependency" / "main.py"
target = "function"
Expand Down