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, })