Back to Ccxt

Advanced Error Handling

wiki/examples/js/advanced-error-handling.md

4.5.601.5 KB
Original Source
javascript
// @NO_AUTO_TRANSPILE
"use strict";
import ccxt from '../../js/ccxt.js';
// instantiate the exchange
let exchange = new ccxt.coinbaseexchange({
    'apiKey': 'XXXXXXXXXXXXXX',
    'secret': 'YYYYYYYYYYYYYY',
});
async function checkOrders() {
    try {
        // fetch orders
        let orders = await exchange.fetchOrders('BTC/USDT');
        // output the result
        console.log(exchange.id, 'fetched orders', orders);
    }
    catch (e) {
        if (e instanceof ccxt.DDoSProtection || e.message.includes('ECONNRESET')) {
            console.log('[DDoS Protection] ' + e.message);
        }
        else if (e instanceof ccxt.RequestTimeout) {
            console.log('[Request Timeout] ' + e.message);
        }
        else if (e instanceof ccxt.AuthenticationError) {
            console.log('[Authentication Error] ' + e.message);
        }
        else if (e instanceof ccxt.ExchangeNotAvailable) {
            console.log('[Exchange Not Available Error] ' + e.message);
        }
        else if (e instanceof ccxt.ExchangeError) {
            console.log('[Exchange Error] ' + e.message);
        }
        else if (e instanceof ccxt.NetworkError) {
            console.log('[Network Error] ' + e.message);
        }
        else {
            // you can throw it if you want to stop the execution
            // console.log ('[Exception ' + e.constructor.name + '] ' + e.message);
            throw e;
        }
    }
}
//  for demonstrational purposes, we use 1000 ms interval
setInterval(checkOrders, 1000);