Skip to content

Commit 9bb8474

Browse files
authored
Improve errors in UDF app (#92)
1 parent 66a00b6 commit 9bb8474

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

singlestoredb/functions/ext/asgi.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,22 @@ class Application(object):
708708
# Error response start
709709
error_response_dict: Dict[str, Any] = dict(
710710
type='http.response.start',
711-
status=401,
712-
headers=[(b'content-type', b'text/plain')],
711+
status=500,
712+
headers=[(b'content-type', b'application/json')],
713+
)
714+
715+
# Timeout response start
716+
timeout_response_dict: Dict[str, Any] = dict(
717+
type='http.response.start',
718+
status=504,
719+
headers=[(b'content-type', b'application/json')],
720+
)
721+
722+
# Cancel response start
723+
cancel_response_dict: Dict[str, Any] = dict(
724+
type='http.response.start',
725+
status=503,
726+
headers=[(b'content-type', b'application/json')],
713727
)
714728

715729
# JSON response start
@@ -1233,12 +1247,14 @@ async def __call__(
12331247
'timeout': func_info['timeout'],
12341248
},
12351249
)
1236-
body = (
1237-
'[TimeoutError] Function call timed out after ' +
1238-
str(func_info['timeout']) +
1239-
' seconds'
1250+
body = json.dumps(
1251+
dict(
1252+
error='[TimeoutError] Function call timed out after ' +
1253+
str(func_info['timeout']) +
1254+
' seconds',
1255+
),
12401256
).encode('utf-8')
1241-
await send(self.error_response_dict)
1257+
await send(self.timeout_response_dict)
12421258

12431259
except asyncio.CancelledError:
12441260
self.logger.exception(
@@ -1249,8 +1265,12 @@ async def __call__(
12491265
'function_name': func_name.decode('utf-8'),
12501266
},
12511267
)
1252-
body = b'[CancelledError] Function call was cancelled'
1253-
await send(self.error_response_dict)
1268+
body = json.dumps(
1269+
dict(
1270+
error='[CancelledError] Function call was cancelled',
1271+
),
1272+
).encode('utf-8')
1273+
await send(self.cancel_response_dict)
12541274

12551275
except Exception as e:
12561276
self.logger.exception(
@@ -1262,7 +1282,11 @@ async def __call__(
12621282
'exception_type': type(e).__name__,
12631283
},
12641284
)
1265-
body = f'[{type(e).__name__}] {str(e).strip()}'.encode('utf-8')
1285+
body = json.dumps(
1286+
dict(
1287+
error=f'[{type(e).__name__}] {str(e).strip()}',
1288+
),
1289+
).encode('utf-8')
12661290
await send(self.error_response_dict)
12671291

12681292
finally:

0 commit comments

Comments
 (0)