Skip to content

Commit e0720d5

Browse files
authored
fix: Fail on start() instead of silent ignore; encode string during error returns (#85)
add try/except wrapper for start() method to error on failed start; encode strings to bytes in exception returns
1 parent cc494b2 commit e0720d5

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎src/func_python/cloudevent.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ async def __call__(self, scope, receive, send):
114114
while True:
115115
message = await receive()
116116
if message['type'] == 'lifespan.startup':
117-
await self.on_start()
117+
try:
118+
await self.on_start()
119+
except Exception as e:
120+
logging.error("function startup failed: %s", e)
121+
await send({'type': 'lifespan.startup.failed',
122+
'message': str(e)})
123+
return
118124
await send({'type': 'lifespan.startup.complete'})
119125
elif message['type'] == 'lifespan.shutdown':
120126
await self.on_stop()
@@ -253,7 +259,7 @@ async def send_exception(send, code, message):
253259
'headers': [[b'content-type', b'text/plain']],
254260
})
255261
await send({
256-
'type': 'http.response.body', 'body': message,
262+
'type': 'http.response.body', 'body': message.encode('utf-8') if isinstance(message, str) else message,
257263
})
258264

259265

‎src/func_python/http.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ async def __call__(self, scope, receive, send):
107107
while True:
108108
message = await receive()
109109
if message['type'] == 'lifespan.startup':
110-
await self.on_start()
110+
try:
111+
await self.on_start()
112+
except Exception as e:
113+
logging.error("function startup failed: %s", e)
114+
await send({'type': 'lifespan.startup.failed',
115+
'message': str(e)})
116+
return
111117
await send({'type': 'lifespan.startup.complete'})
112118
elif message['type'] == 'lifespan.shutdown':
113119
await self.on_stop()
@@ -186,5 +192,5 @@ async def send_exception(send, code, message):
186192
'headers': [[b'content-type', b'text/plain']],
187193
})
188194
await send({
189-
'type': 'http.response.body', 'body': message,
195+
'type': 'http.response.body', 'body': message.encode('utf-8') if isinstance(message, str) else message,
190196
})

0 commit comments

Comments
 (0)