diff --git a/package.json b/package.json index 2936c50..60f3eca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.17.35", + "version": "0.17.36", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/__tests__/orionAggregator.test.ts b/src/__tests__/orionAggregator.test.ts index 9384d49..c09a6c6 100644 --- a/src/__tests__/orionAggregator.test.ts +++ b/src/__tests__/orionAggregator.test.ts @@ -1,7 +1,7 @@ import Orion from '../Orion'; describe('Orion Aggregator', () => { - test('Handle error', async () => { + test('Handle error aus', async () => { const orion = new Orion('testing'); const bscUnit = orion.getUnit('bsc') @@ -27,4 +27,30 @@ describe('Orion Aggregator', () => { }) }); }); + + test('Handle error aobus', async () => { + const orion = new Orion('testing'); + const bscUnit = orion.getUnit('bsc') + + let subId: string; + + await new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + bscUnit.orionAggregator.ws.unsubscribe(subId); + bscUnit.orionAggregator.ws.destroy() + reject(new Error('Timeout')); + }, 10000); + const payload = 'BTCUSDF'; + subId = bscUnit.orionAggregator.ws.subscribe('aobus', { + payload, + callback: () => null, + errorCb: (message) => { + console.log(message); + clearTimeout(timeout); + bscUnit.orionAggregator.ws.destroy() + resolve(true); + } + }) + }); + }); }); diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index ad56f2b..be45636 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -370,13 +370,15 @@ class OrionAggregatorWS { // Get subscription error callback // 2. Find subscription by id // 3. Call onError callback - - const subType = objectKeys(this.subscriptions).find((st) => this.subscriptions[st]?.[err.id]); - if (subType === undefined) throw new Error('OrionAggregatorWS: cannot find subscription type by id'); - const sub = this.subscriptions[subType]?.[err.id]; - if (sub === undefined) throw new Error('OrionAggregatorWS: cannot find subscription by id'); - if ('errorCb' in sub) { - sub.errorCb(err.m); + const { id } = err; + if (id !== undefined) { + const subType = objectKeys(this.subscriptions).find((st) => this.subscriptions[st]?.[id]); + if (subType === undefined) throw new Error('OrionAggregatorWS: cannot find subscription type by id'); + const sub = this.subscriptions[subType]?.[id]; + if (sub === undefined) throw new Error('OrionAggregatorWS: cannot find subscription by id'); + if ('errorCb' in sub) { + sub.errorCb(err.m); + } } this.onError?.(err.m); } diff --git a/src/services/OrionAggregator/ws/schemas/errorSchema.ts b/src/services/OrionAggregator/ws/schemas/errorSchema.ts index a9ec03b..370d9fa 100644 --- a/src/services/OrionAggregator/ws/schemas/errorSchema.ts +++ b/src/services/OrionAggregator/ws/schemas/errorSchema.ts @@ -5,7 +5,7 @@ import baseMessageSchema from './baseMessageSchema'; const errorSchema = baseMessageSchema.extend({ T: z.literal(MessageType.ERROR), c: z.number().int(), // code - id: z.string(), // subscription id + id: z.string().optional(), // subscription id m: z.string(), // error message, });