Skip to content

Commit 2adbf72

Browse files
committed
fix: respond to PR comments
1 parent 7ee157b commit 2adbf72

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/functions_framework/aio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ async def __call__(self, scope, receive, send):
218218
"body": b"Internal Server Error",
219219
}
220220
)
221-
# Don't re-raise to prevent starlette from printing tracebak again
221+
# Don't re-raise to prevent starlette from printing traceback again
222222

223223

224224
def create_asgi_app(target=None, source=None, signature_type=None):

src/functions_framework/execution_id.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ def _generate_execution_id():
7777

7878
def _extract_context_from_headers(headers):
7979
"""Extract execution context from request headers."""
80-
execution_id = headers.get(EXECUTION_ID_REQUEST_HEADER)
81-
8280
trace_context = re.match(
8381
_TRACE_CONTEXT_REGEX_PATTERN,
8482
headers.get(TRACE_CONTEXT_REQUEST_HEADER, ""),
8583
)
84+
execution_id = headers.get(EXECUTION_ID_REQUEST_HEADER)
8685
span_id = trace_context.group("span_id") if trace_context else None
8786

8887
return ExecutionContext(execution_id, span_id)
8988

9089

90+
# Middleware to add execution id to request header if one does not already exist
9191
class WsgiMiddleware:
9292
def __init__(self, wsgi_app):
9393
self.wsgi_app = wsgi_app

tests/test_execution_id_async.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,27 @@ def test_sync_cloudevent_function_has_execution_context(monkeypatch, capsys):
339339
record = capsys.readouterr()
340340
assert f"Execution ID in sync CloudEvent: {TEST_EXECUTION_ID}" in record.err
341341
assert "No execution context in sync CloudEvent function!" not in record.err
342+
343+
344+
def test_cloudevent_returns_500(capsys, monkeypatch):
345+
monkeypatch.setenv("LOG_EXECUTION_ID", "true")
346+
source = TEST_FUNCTIONS_DIR / "execution_id" / "async_main.py"
347+
target = "async_cloudevent_error"
348+
app = create_asgi_app(target, source, signature_type="cloudevent")
349+
client = TestClient(app, raise_server_exceptions=False)
350+
resp = client.post(
351+
"/",
352+
headers={
353+
"ce-specversion": "1.0",
354+
"ce-type": "com.example.test",
355+
"ce-source": "test-source",
356+
"ce-id": "test-id",
357+
"Function-Execution-Id": TEST_EXECUTION_ID,
358+
"Content-Type": "application/json",
359+
},
360+
)
361+
assert resp.status_code == 500
362+
record = capsys.readouterr()
363+
assert f'"execution_id": "{TEST_EXECUTION_ID}"' in record.err
364+
assert '"logging.googleapis.com/labels"' in record.err
365+
assert "ValueError" in record.err

tests/test_functions/execution_id/async_main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ def sync_cloudevent_with_context(cloud_event):
5656
logger.info(f"Execution ID in sync CloudEvent: {context.execution_id}")
5757
else:
5858
logger.error("No execution context in sync CloudEvent function!")
59+
60+
61+
async def async_cloudevent_error(cloudevent):
62+
raise ValueError("This is a test error")

0 commit comments

Comments
 (0)