Tinkoff / invest-openapi-js-sdk

Apache License 2.0
211 stars 50 forks source link

Обработка ошибок streaming протокола #83

Closed l1bbcsg closed 3 years ago

l1bbcsg commented 4 years ago

В WS протоколе предусмотрено событие сообщения об ошибке , однако сейчас оно не обрабатывается и вообще отваливается с исключением Error: Unknown type: error.

Помимо обработки сообщения ещё не хватает в общем-то и апи для подписки на него.

Пример:

const secretToken = `...`;
const apiURL = 'https://api-invest.tinkoff.ru/openapi/sandbox';
const socketURL = 'wss://api-invest.tinkoff.ru/openapi/md/v1/md-openapi/ws';

import OpenAPI from '@tinkoff/invest-openapi-js-sdk';

const api = new OpenAPI({ apiURL, secretToken, socketURL });

api.orderbook({figi: 'DOES-NOT-EXIST'}, console.log)

Падает с:

/.../node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:135
        throw new Error(`Unknown type: ${type}`);
    at Streaming.getEventName (/.../node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:135:15)
    at WebSocket.Streaming.handleSocketMessage (/.../node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:97:28)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/.../node_modules/ws/lib/websocket.js:800:20)
    at Receiver.emit (events.js:315:20)
    at Receiver.dataMessage (/.../node_modules/ws/lib/receiver.js:436:14)
    at Receiver.getData (/.../node_modules/ws/lib/receiver.js:366:17)
    at Receiver.startLoop (/.../node_modules/ws/lib/receiver.js:142:22)
    at Receiver._write (/.../node_modules/ws/lib/receiver.js:77:10)
    at writeOrBuffer (_stream_writable.js:352:12)

Хотя при этом в дебаггере можно обнаружить сообщение:

"{"payload":{"error":"Subscription orderbook:subscribe. FIGI DOES-NOT-EXIST not found"},"event":"error","time":"2020-08-01T16:08:32.792175244Z"}"
AlShu commented 4 years ago

при подписке на след иструменты падает с этой же ошибкой

[ { figi: 'TCS000C8NJ10', ticker: 'POLold', isin: 'US73179P1066', lot: 1, currency: 'USD', name: 'PolyOne Corp', type: 'Stock' }, { figi: 'TCS000BKQG80', ticker: 'IAC_old', isin: 'US44919P5089', lot: 1, currency: 'USD', name: 'IAC InterActiveCorp', type: 'Stock' } ]

/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:136 throw new Error(Unknown type: ${type}); ^

Error: Unknown type: error at Streaming.getEventName (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:136:15) at WebSocket.Streaming.handleSocketMessage (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/build/OpenAPI.cjs.js:97:28) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/node_modules/ws/lib/websocket.js:797:20) at Receiver.emit (events.js:315:20) at Receiver.dataMessage (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/node_modules/ws/lib/receiver.js:437:14) at Receiver.getData (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/node_modules/ws/lib/receiver.js:367:17) at Receiver.startLoop (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/node_modules/ws/lib/receiver.js:143:22) at Receiver._write (/Users/shugurov/projects/invest/tinkoff-2/node_modules/@tinkoff/invest-openapi-js-sdk/node_modules/ws/lib/receiver.js:78:10) at doWrite (_stream_writable.js:392:12)

для всех остальных инструментов этот код работает:

async function runInstrument(instrument: any) {
    const {figi, ticker} = instrument;
    console.log('subscribing to order book');
    const unsub = api.orderbook({figi, depth: 10}, (x) => {
        console.log(x);
    });
    console.log('subscribed to order book ${ticker}');

    console.log('subscribing to order book');
    const unsubCandle = api.candle({figi, interval: '1min'}, (x) => {
        console.log(x);
    });
    console.log('subscribed to Candle ${ticker}');
}
betslus1 commented 3 years ago

https://github.com/TinkoffCreditSystems/invest-openapi-js-sdk/issues/117 тут исправлено