Add error handler for exceptions raised in subscription sources
@@ -185,6 +185,7 @@ class GraphQL:
async def observe_async_results(
self, results: AsyncGenerator, operation_id: str, websocket: WebSocket
) -> None:
+ try:
async for result in results:
payload = {}
if result.data:
@@ -196,5 +197,12 @@ class GraphQL:
await websocket.send_json(
{"type": GQL_DATA, "id": operation_id, "payload": payload}
)
+ except Exception as error:
+ graphql_error = GraphQLError(str(error), original_error=error)
+ payload = {"errors": [format_error(graphql_error, debug=self.debug)]}
+ await websocket.send_json(
+ {"type": GQL_DATA, "id": operation_id, "payload": payload}
+ )
+
if websocket.application_state != WebSocketState.DISCONNECTED:
await websocket.send_json({"type": GQL_COMPLETE, "id": operation_id})
