diff --git a/src/snek/view/rpc.py b/src/snek/view/rpc.py index 5a600e3..4372e34 100644 --- a/src/snek/view/rpc.py +++ b/src/snek/view/rpc.py @@ -115,7 +115,7 @@ class RPCView(BaseView): raise Exception("Not allowed") args = data.get("args") if hasattr(super(),method_name) or not hasattr(self,method_name): - return await self.ws.send_json({"callId":call_id,"data":"Not allowed"}) + return await self._send_json({"callId":call_id,"data":"Not allowed"}) method = getattr(self,method_name.replace(".","_"),None) if not method: raise Exception("Method not found") @@ -125,9 +125,12 @@ class RPCView(BaseView): result = dict({"exception":str(ex),"traceback":traceback.format_exc()}) print(result,flush=True) #dict(error=ex=str(ex),traceback=traceback.format_exc()) - await self.ws.send_json({"callId":call_id,"success":True,"data":result}) + await self._send_json({"callId":call_id,"success":True,"data":result}) except Exception as ex: - await self.ws.send_json({"callId":call_id,"success":False,"data":str(ex)}) + await self._send_json({"callId":call_id,"success":False,"data":str(ex)}) + + async def _send_json(self,obj): + await self.ws.send_text(json.dumps(obj,default=str)) async def call_ping(self,callId,*args): return {"pong": args} @@ -147,7 +150,13 @@ class RPCView(BaseView): async for msg in ws: print(msg) if msg.type == web.WSMsgType.TEXT: - await rpc(msg.json()) + try: + await rpc(msg.json()) + except Exception as ex: + print(ex,flush=True) + print(traceback.format_exc(),flush=True) + await self.services.socket.delete(ws) + break elif msg.type == web.WSMsgType.ERROR: print(f"WebSocket exception {ws.exception()}") await self.services.socket.delete(ws)