XRPL-Labs / xrpl-client

Connect to the XRP Ledger using WebSockets
https://www.npmjs.com/package/xrpl-client
MIT License
12 stars 8 forks source link

Error Handling ( Max. connection attempts exceeded ) #1

Closed daniele-bolla closed 2 years ago

daniele-bolla commented 2 years ago

Explanation

Hello i would like to ask how can i handle errors in a clean way. In particular I need to catch Max. connection attempts exceeded but it seems that the xrpClient doesn't throw an error ( Maybe i am wrong ). Also i tried to subscribe to different websocket or eventemitter events, but i am not sure how to handle this situation with them. Is it possible to get an example?

Code

const main = async (walletAddress: string, network: string)  => {
  const X_url = network;
  const xrpClient = new XrplClient(X_url, {
    assumeOfflineAfterSeconds: 15,
    maxConnectionAttempts: 4,
    connectAttemptTimeoutSeconds: 4,
  });
  console.log(
    xrpClient.eventBus.on("__WsClient_close", () => {
      console.log("__WsClient_close");
    })
  );
  xrpClient.on("state", (state) => {
    console.log("state", state);
  });
  xrpClient.on("message", (message) => {
    console.log("message", message);
  });
  xrpClient.on("ledger", (ledger) => {
    console.log("Ledger", ledger);
  });
  xrpClient.on("close", (close) => {
    console.log("close", close);
  });
  const connectionState = xrpClient.getState();
  console.log("connectionState", connectionState);
  await xrpClient.ready();
  const serverInfo = await xrpClient.send({ command: "server_info" });
  console.log("serverInfo", serverInfo);
};

(async () => {
        try {
           await main("walletAddress", "network");
        } catch (err) {
          console.log(err); //No Error here
        } 
})();

Error

browser.min.js:13 Uncaught Error: Max. connection attempts exceeded
    at s.A (browser.min.js:13)
N3TC4T commented 2 years ago

Hey @daniele-bolla

for handling the errors you can use event:

xrpClient.on("error", console.log)
daniele-bolla commented 2 years ago

Thanks! @N3TC4T