fix(data): P1 #4 WS 异常事件监听 + #7 tradingPairs 类型修复

- #4: ws.ts 新增 exception 事件监听,logger.error + system:error
- #7: exchange.entity.ts 使用 import type 破循环依赖,unknown[] → TradingPair[]
This commit is contained in:
Rekey
2026-06-18 18:20:42 +08:00
parent 0e449d3c5f
commit 77ffc67a25
3 changed files with 13 additions and 7 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ import {
OneToMany,
} from "typeorm";
import { CommonBaseEntity } from "./common.entity";
import type { TradingPair } from "./trading-pair.entity";
@Entity("exchanges")
export class Exchange extends CommonBaseEntity {
@@ -37,5 +38,5 @@ export class Exchange extends CommonBaseEntity {
* 使用字符串引用避免循环依赖(TradingPair 也引用 Exchange)。
*/
@OneToMany("TradingPair", "exchange")
tradingPairs!: unknown[];
tradingPairs!: TradingPair[];
}
+5 -1
View File
@@ -1,6 +1,6 @@
import { WebsocketClient, type WsMessageKlineFormatted } from "binance";
import { bus } from "../../utils";
import { bus, logger } from "../../utils";
import { BaseWsClient } from "../base";
import type { Kline, PairType } from "../../types";
@@ -40,6 +40,10 @@ export class BinanceWsClient extends BaseWsClient {
this.ws.on("open", () => bus.emit("ws:connected"));
this.ws.on("close", () => bus.emit("ws:disconnected"));
this.ws.on("exception", (err) => {
logger.error({ exchange: this.exchange, type: this.type, err }, "Binance WS 异常");
bus.emit("system:error", { source: "binance:ws", error: err, context: { exchange: this.exchange, type: this.type } });
});
}
watch(symbol: string): void {
+6 -5
View File
@@ -21,10 +21,11 @@
## 🟡 P1 — 尽快修复
- [ ] **4. WS 客户端无 `error` 事件监听**
- [x] **4. WS 客户端无异常事件监听**
- 位置:`data/exchanges/binance/ws.ts:41-42`
- 问题:仅监听 `open` / `close`,未监听 `error`。SDK 内部异常(如解析失败、协议错误)静默丢失
- 建议:添加 `this.ws.on("error", ...)` 并 emit `system:error`
- 问题:仅监听 `open` / `close`,未监听 SDK 异常事件。SDK 内部异常(如解析失败、协议错误)静默丢失
- SDK 现状:`BaseWebsocketClient` 定义了两个事件 — `error`(已废弃,会意外导致 unhandled rejection)和 `exception`(推荐使用)。`exception` 回调参数为 `response: any & { wsKey: string; isWSAPIResponse?: boolean }`
- 方案:在 `BinanceWsClient` 构造器中 `this.ws.on("close", ...)` 后追加 `this.ws.on("exception", (err) => { logger.error({ exchange: this.exchange, type: this.type, err }, "Binance WS 异常"); bus.emit("system:error", { source: "binance:ws", error: err, context: { exchange: this.exchange, type: this.type } }); })`。需新增 `logger` 导入。`system:error` 已在 `BusEvents` 中定义,无需改 bus。消费者(Telegram 通知等)后续接入
- [ ] **5. REST 请求无显式 HTTP 错误处理**
- 位置:`data/exchanges/binance/rest.ts:109-115`
@@ -36,10 +37,10 @@
- 问题:每条日志执行 `new Error().stack` 解析调用位置,生产环境高频日志下 CPU 开销 0.1-0.5ms/条
- 建议:仅在 `logging.nodeEnv === "development"` 时执行
- [ ] **7. `exchange.entity.ts` 中 `tradingPairs` 类型错误**
- [x] **7. `exchange.entity.ts` 中 `tradingPairs` 类型错误**
- 位置:`data/db/entities/exchange.entity.ts:40`
- 问题:`tradingPairs!: unknown[]` 应为 `TradingPair[]``unknown[]` 丢失类型信息
- 建议:改为 `TradingPair[]` 并确保 import 正确
- 方案:`Exchange``TradingPair` 存在循环依赖(`TradingPair` `import { Exchange }`),使用 `import type { TradingPair } from "./trading-pair.entity"` 仅在编译期引入类型,编译后擦除,不产生运行时循环依赖。同时将 L40 改为 `tradingPairs!: TradingPair[]`
## 🟢 P2 — 计划改进