anurag008w commited on
Commit
f72a7b7
·
1 Parent(s): 78eaac4

Enhance error handling for WebSocket operations

Browse files

Added error handling for WebSocket send operation and unhandled promise rejections.

Files changed (1) hide show
  1. wa-guardian.js +14 -1
wa-guardian.js CHANGED
@@ -141,7 +141,13 @@ async function callRpc(ws, method, params) {
141
  }
142
  };
143
  ws.on("message", handler);
144
- ws.send(JSON.stringify({ type: "req", id, method, params }));
 
 
 
 
 
 
145
  setTimeout(() => { ws.removeListener("message", handler); reject(new Error("RPC Timeout")); }, WAIT_TIMEOUT + 5000);
146
  });
147
  }
@@ -247,6 +253,13 @@ if (!WHATSAPP_ENABLED) {
247
  process.exit(0);
248
  }
249
 
 
 
 
 
 
 
 
250
  writeStatus({ configured: true, connected: false, pairing: false });
251
  console.log("[guardian] WhatsApp Guardian active. Monitoring pairing status...");
252
  setInterval(checkStatus, CHECK_INTERVAL);
 
141
  }
142
  };
143
  ws.on("message", handler);
144
+ try {
145
+ ws.send(JSON.stringify({ type: "req", id, method, params }));
146
+ } catch (sendErr) {
147
+ ws.removeListener("message", handler);
148
+ reject(sendErr);
149
+ return;
150
+ }
151
  setTimeout(() => { ws.removeListener("message", handler); reject(new Error("RPC Timeout")); }, WAIT_TIMEOUT + 5000);
152
  });
153
  }
 
253
  process.exit(0);
254
  }
255
 
256
+ process.on("unhandledRejection", (reason) => {
257
+ const msg = reason && reason.message ? reason.message : String(reason);
258
+ if (!/RPC Timeout|Timeout/i.test(msg)) {
259
+ console.log(`[guardian] Unhandled rejection: ${msg}`);
260
+ }
261
+ });
262
+
263
  writeStatus({ configured: true, connected: false, pairing: false });
264
  console.log("[guardian] WhatsApp Guardian active. Monitoring pairing status...");
265
  setInterval(checkStatus, CHECK_INTERVAL);