From ac087c1078836a59d9f3fa2c064c5b4dd144d0da Mon Sep 17 00:00:00 2001 From: David Fridrich Date: Wed, 6 May 2026 09:09:58 +0200 Subject: [PATCH] add try/except wrapper for start() method to error on failed start; encode strings to bytes in exception returns --- src/func_python/cloudevent.py | 10 ++++++++-- src/func_python/http.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/func_python/cloudevent.py b/src/func_python/cloudevent.py index 8465803a..6296c53c 100644 --- a/src/func_python/cloudevent.py +++ b/src/func_python/cloudevent.py @@ -114,7 +114,13 @@ async def __call__(self, scope, receive, send): while True: message = await receive() if message['type'] == 'lifespan.startup': - await self.on_start() + try: + await self.on_start() + except Exception as e: + logging.error("function startup failed: %s", e) + await send({'type': 'lifespan.startup.failed', + 'message': str(e)}) + return await send({'type': 'lifespan.startup.complete'}) elif message['type'] == 'lifespan.shutdown': await self.on_stop() @@ -253,7 +259,7 @@ async def send_exception(send, code, message): 'headers': [[b'content-type', b'text/plain']], }) await send({ - 'type': 'http.response.body', 'body': message, + 'type': 'http.response.body', 'body': message.encode('utf-8') if isinstance(message, str) else message, }) diff --git a/src/func_python/http.py b/src/func_python/http.py index 4133f51d..a96d603c 100644 --- a/src/func_python/http.py +++ b/src/func_python/http.py @@ -107,7 +107,13 @@ async def __call__(self, scope, receive, send): while True: message = await receive() if message['type'] == 'lifespan.startup': - await self.on_start() + try: + await self.on_start() + except Exception as e: + logging.error("function startup failed: %s", e) + await send({'type': 'lifespan.startup.failed', + 'message': str(e)}) + return await send({'type': 'lifespan.startup.complete'}) elif message['type'] == 'lifespan.shutdown': await self.on_stop() @@ -186,5 +192,5 @@ async def send_exception(send, code, message): 'headers': [[b'content-type', b'text/plain']], }) await send({ - 'type': 'http.response.body', 'body': message, + 'type': 'http.response.body', 'body': message.encode('utf-8') if isinstance(message, str) else message, })