feat(v4.0.0-beta.5): BREAKING CHANGE: rename "error" event to "exception" to avoid unhandled exceptions

This commit is contained in:
tiagosiebler
2025-02-06 12:08:51 +00:00
parent a8f8d6bf15
commit 57b1a72b7f
10 changed files with 42 additions and 45 deletions

View File

@@ -23,6 +23,8 @@ import {
} from './websockets';
import { WsOperation } from '../types/websockets/ws-api';
type UseTheExceptionEventInstead = never;
interface WSClientEventMap<WsKey extends string> {
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
open: (evt: { wsKey: WsKey; event: any }) => void;
@@ -39,10 +41,10 @@ interface WSClientEventMap<WsKey extends string> {
/** Received data for topic */
update: (response: any & { wsKey: WsKey }) => void;
/**
* Exception from ws client OR custom listeners (e.g. if you throw inside your event handler)
* @deprecated Use 'exception' instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
* See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
* @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
*/
error: (response: any & { wsKey: WsKey; isWSAPIResponse?: boolean }) => void;
error: UseTheExceptionEventInstead;
/**
* Exception from ws client OR custom listeners (e.g. if you throw inside your event handler)
*/
@@ -57,12 +59,6 @@ interface WSClientEventMap<WsKey extends string> {
}) => void;
}
export interface EmittableEvent<TEvent = any> {
eventType: 'response' | 'update' | 'error' | 'authenticated';
event: TEvent;
isWSAPIResponse?: boolean;
}
// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
export interface BaseWebsocketClient<
TWSKey extends string,
@@ -80,6 +76,12 @@ export interface BaseWebsocketClient<
): boolean;
}
export interface EmittableEvent<TEvent = any> {
eventType: 'response' | 'update' | 'exception' | 'authenticated';
event: TEvent;
isWSAPIResponse?: boolean;
}
/**
* A midflight WS request event (e.g. subscribe to these topics).
*
@@ -167,12 +169,6 @@ export abstract class BaseWebsocketClient<
authPrivateRequests: false,
...options,
};
// add default error handling so this doesn't crash node (if the user didn't set a handler)
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars, no-unused-vars
this.on('error', (e) => {
// console.log('basewserr: ', e);
});
}
/**
@@ -593,7 +589,7 @@ export abstract class BaseWebsocketClient<
if (!error.message) {
this.logger.error(`${context} due to unexpected error: `, error);
this.emit('response', { ...error, wsKey });
this.emit('error', { ...error, wsKey });
this.emit('exception', { ...error, wsKey });
return;
}
@@ -628,7 +624,7 @@ export abstract class BaseWebsocketClient<
this.logger.error(`parseWsError(${context}, ${error}, ${wsKey}) `, error);
this.emit('response', { ...error, wsKey });
this.emit('error', { ...error, wsKey });
this.emit('exception', { ...error, wsKey });
}
/** Get a signature, build the auth request and send it */

View File

@@ -136,7 +136,7 @@ export class WebsocketClient extends BaseWebsocketClient<
perWsKeyTopics[derivedWsKey] = [];
}
perWsKeyTopics[derivedWsKey].push(wsRequest);
perWsKeyTopics[derivedWsKey]!.push(wsRequest);
}
const promises: Promise<unknown>[] = [];
@@ -755,7 +755,7 @@ export class WebsocketClient extends BaseWebsocketClient<
}
results.push({
eventType: 'error',
eventType: 'exception',
event: parsed,
isWSAPIResponse: true,
});
@@ -804,7 +804,7 @@ export class WebsocketClient extends BaseWebsocketClient<
// Failed request
if (parsed.success === false) {
results.push({
eventType: 'error',
eventType: 'exception',
event: parsed,
});
return results;
@@ -851,7 +851,7 @@ export class WebsocketClient extends BaseWebsocketClient<
exception: e,
eventData: event.data,
},
eventType: 'error',
eventType: 'exception',
});
this.logger.error('Failed to parse event data due to exception: ', {