Fix error schema

This commit is contained in:
Aleksandr Kraiz
2023-03-15 14:33:24 +04:00
parent 6c16403048
commit 0c298fcd76
4 changed files with 38 additions and 10 deletions

View File

@@ -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",

View File

@@ -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);
}
})
});
});
});

View File

@@ -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);
}

View File

@@ -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,
});