ccxt: some advice on error handling in JS

Could you add some examples to the manual that show how to detect what the cause of an exception is?

I’m seeing some errors being thrown and in the stack i see things like [Function: OrderNotFound] and [Function: RequestTimeout]. It would be nice to be able to trap these in order to respond to them appropriately. How do you do this in javascript?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (20 by maintainers)

Commits related to this issue

Most upvoted comments

@npomfret just in case, adding these lines before the call to editOrder might help figure this out or shed some light on this at the very least:


       // -------------------------------------------------------
       // ADD THESE LINES ↓

       const { InvalidOrder } = require ('ccxt')

       console.log ({
            'InvalidOrder class reference': ccxt.InvalidOrder,
            'ccxt version': ccxt.version,
            'InvalidOrder': InvalidOrder,
            "require ('ccxt').InvalidOrder": (require ('ccxt')).InvalidOrder,
            "require ('ccxt').version": (require ('ccxt')).version,
       })

       // ↑ ADD THESE LINES
       // -------------------------------------------------------
        
        const response = await exchange.editOrder(id, symbol, type, side, amount, price)

Ok, my system tried to edit a partially filled order just now. I had your logging in place:

{ orderId: '536076653',
  'e.constructor.name': 'InvalidOrder',
  'is ExchangeError': false,
  'is NetworkError': false,
  'is InvalidOrder': false,
  'exception itself': { Error: liquid {"errors":{"order":["Can not update partially filled order"]}}
    at liquid.handleErrors (/Users/nickpomfret/projects/CrypPro-marketdata/gae-services/ticking-orderbook-service/node_modules/ccxt/js/liquid.js:687:31)
    at response.text.then (/Users/nickpomfret/projects/CrypPro-marketdata/gae-services/ticking-orderbook-service/node_modules/ccxt/js/base/Exchange.js:648:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7) constructor: [Function: InvalidOrder] } }

What’s harder to test is editing an open order that’s partially filled. If it happens ill do my best to capture the output.

Looks good now. Using your code above with the latest ccxt code:

{ 'e.constructor.name': 'InvalidOrder',
  'is ExchangeError': true,
  'is NetworkError': false,
  'is InvalidOrder': true,
  'exception itself': { Error: liquid {"errors":{"order":["Can not update partially filled order","Can not update non-live order"]}}
    at liquid.handleErrors (/Users/crypro/projects/CrypPro-marketdata/exchange-connectivity/node_modules/ccxt/js/liquid.js:687:31)
    at response.text.then (/Users/crypro/projects/CrypPro-marketdata/exchange-connectivity/node_modules/ccxt/js/base/Exchange.js:648:18)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7) constructor: [Function: InvalidOrder] } }

Here’s the verbose output of cancelling an already cancelled order

 liquid PUT https://api.liquid.com/orders/528725347/cancel 200 OK 
Response:
 { Date: 'Thu, 25 Oct 2018 20:24:27 GMT',
  'Content-Type': 'application/json; charset=utf-8',
  'Content-Length': '311',
  Connection: 'close',
  'Cache-Control': 'max-age=0, private, must-revalidate',
  'Content-Encoding': 'gzip',
  Etag: '"599746de5823c031dd5618e30a171138"',
  Status: '200 OK',
  'Strict-Transport-Security': 'max-age=631152000; includeSubdomains',
  Vary: 'Accept-Encoding',
  Via: '1.1 spaces-router (4b147c19bd8b)',
  'X-Content-Type-Options': 'nosniff',
  'X-Frame-Options': 'SAMEORIGIN',
  'X-Powered-By': 'Phusion Passenger 5.1.2',
  'X-Rack-Cache': 'invalidate, pass',
  'X-Request-Id': '6fc841fb-66f6-7221-2602-823ab191ff71',
  'X-Runtime': '0.061965',
  'X-Ua-Compatible': 'IE=Edge,chrome=1',
  'X-Xss-Protection': '1; mode=block',
  'Expect-Ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  Server: 'cloudflare',
  'Cf-Ray': '46f7681ae9bcbc5c-LHR' } 
 {"id":528725347,"order_type":"limit","quantity":"0.6782002","disc_quantity":"0.0","iceberg_total_quantity":"0.0","side":"sell","filled_quantity":"0.0","price":0.03132615,"created_at":1540498864,"updated_at":1540498966,"status":"cancelled","leverage_level":1,"source_exchange":null,"product_id":37,"product_code":"CASH","funding_currency":"BTC","crypto_account_id":null,"currency_pair_code":"ETHBTC","average_price":"0.0","target":"spot","order_fee":"0.0","source_action":"manual","unwound_trade_id":null,"trade_id":null}