Search code examples
javascriptnode.jserror-handlingtry-catchccxt

Why is my function ending (using external library)


I need your help with some error handling. I'm using an external library but have no clue of what's happening with the error. Here's my code:

//file name = playground.js    
var ccxt = require("ccxt");
    ccxt.exchanges.map(r => {
      let exchange = new ccxt[r]();
      let ticks = exchange
        .fetchTickers()
        .then(res => {
          console.log(res);
        })
        .catch(e => {
          console.log(e);
        });
    });

To execute it correctly, you'll need to install the external library: ccxt via npm: npm i ccxt --save and I get the following error:

.../node_modules/ccxt/js/base/Exchange.js:407
        throw new NotSupported (this.id + ' fetchTickers not supported yet')
        ^

Error: _1broker fetchTickers not supported yet
    at _1broker.fetchTickers (.../node_modules/ccxt/js/base/Exchange.js:407:15)
    at ccxt.exchanges.map.r (.../playground.js:41:6)
    at Array.map (<anonymous>)
    at Object.<anonymous> (.../playground.js:38:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)

Basically, what the library helps me with is:

  • Make automatic request to different servers
  • Organize the responses in uniformed objects
  • Handle most of the errors that are returned by the servers

In my example, the error returned is related with the fact that the server doesn't support the function I'm using. To put it in simpler words: I make a request that server1 might be able to handle but that server2 isn't able to respond to yet.

The ccxt.exhanges in the code returns an Array of the differents servers that are being handled by the library.

The problem is not so much that I get the error... I'm OK with not getting info back from every server but that my function literaly stops once it gets to the error. The .map loop doesn't go all the way to the end...

ccxt publishes some information on Error Handling but I'm not sure what I can do with it (noob in the place sorry guys).

I hope my question is clear enough and hasn't been already asked!

Thanks in advance for your help!


Solution

  • Here's a slightly better version of it:

    var ccxt = require("ccxt");
    ccxt.exchanges.forEach(r => {
    
        let exchange = new ccxt[r]();
    
        if (exchange.hasFetchTickers) { // ← the most significant line
    
            let ticks = exchange
                .fetchTickers()
                .then(res => {
                    console.log(res);
                })
                .catch(e => {
                    console.log(e);
                });
        }  
    });