chore(observability): defensive depth pass
Two related cleanups so unexpected failures stop being invisible:
void-promise wrappers
Every `void this.something()` fire-and-forget call now has a
.catch(err => log.warn(...)) attached. Affected sites:
- packet-pipeline.ts: dispatchAfterIdentityRefresh
- instance.ts: openAll / applyConfigDiff / closeAll / emitEvent
- http-post-adapter.ts: postEvent
- ws-client-adapter.ts: handleApiMessage
Most of the network-manager fan-outs already isolate per-adapter
errors, but the outer catches guard against future refactors and
surface stack traces immediately rather than as unhandled rejection
warnings.
silent-catch sweep (Bridge.Web)
bridge/web/group-honor.ts, group-essence.ts, group-notice.ts had
`} catch (e) { return undefined; }` patterns that turned every QQ
Web API failure into a silent empty response. Now log.warn with
the full stack first, then return the same fallback so behavior is
unchanged. The commented-out `// console.error(e)` debris is gone.
Plus one console → logger conversion in message-parser.ts (music
sign fallback path).
This commit is contained in:
8 files changed
+53
-16
No files matched your search
@@ -75,7 +75,10 @@ export class IncomingPacketPipeline {
|
|||||||
const events = handler(pkt, this.deps.identity);
|
const events = handler(pkt, this.deps.identity);
|
||||||
for (const event of events) {
|
for (const event of events) {
|
||||||
if (this.needsPreDispatchIdentityRefresh(event)) {
|
if (this.needsPreDispatchIdentityRefresh(event)) {
|
||||||
void this.dispatchAfterIdentityRefresh(event);
|
void this.dispatchAfterIdentityRefresh(event).catch((err) => {
|
||||||
|
this.log.warn('dispatchAfterIdentityRefresh failed: %s',
|
||||||
|
err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
this.handleSideEffects(event);
|
this.handleSideEffects(event);
|
||||||
printEvent(this.eventLog, event);
|
printEvent(this.eventLog, event);
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
|
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
|
||||||
|
import { createLogger } from '../../utils/logger';
|
||||||
|
|
||||||
|
const log = createLogger('Bridge.Web');
|
||||||
|
|
||||||
// 定义接口返回类型
|
// 定义接口返回类型
|
||||||
export interface GroupEssenceMsgRet {
|
export interface GroupEssenceMsgRet {
|
||||||
@@ -39,6 +42,8 @@ export async function getGroupEssenceMsg(
|
|||||||
);
|
);
|
||||||
return ret.retcode === 0 ? ret : undefined;
|
return ret.retcode === 0 ? ret : undefined;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.warn('getGroupEssenceMsg failed (group=%s page=%d/%d): %s',
|
||||||
|
groupCode, pageStart, pageLimit, e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import { RequestUtil, cookieToString } from './request-util';
|
import { RequestUtil, cookieToString } from './request-util';
|
||||||
|
import { createLogger } from '../../utils/logger';
|
||||||
|
|
||||||
|
const log = createLogger('Bridge.Web');
|
||||||
|
|
||||||
export enum WebHonorType {
|
export enum WebHonorType {
|
||||||
TALKATIVE = 'talkative',
|
TALKATIVE = 'talkative',
|
||||||
@@ -43,7 +46,8 @@ export async function getHonorListWebAPI(cookieObject: Record<string, string>, g
|
|||||||
description: item?.desc,
|
description: item?.desc,
|
||||||
}));
|
}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// console.error(e);
|
log.warn('getHonorListWebAPI failed (group=%s type=%d): %s',
|
||||||
|
groupCode, type, e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { RequestUtil, cookieToString } from './request-util';
|
import { RequestUtil, cookieToString } from './request-util';
|
||||||
import https from 'node:https';
|
import https from 'node:https';
|
||||||
// import { readFileSync } from 'node:fs';
|
import { createLogger } from '../../utils/logger';
|
||||||
// import * as console from "node:console";
|
|
||||||
|
const log = createLogger('Bridge.Web');
|
||||||
|
|
||||||
export interface SetNoticeRetSuccess {
|
export interface SetNoticeRetSuccess {
|
||||||
ec?: number;
|
ec?: number;
|
||||||
@@ -112,6 +113,7 @@ export async function setGroupNoticeWebAPI(
|
|||||||
);
|
);
|
||||||
return ret;
|
return ret;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
log.warn('group-notice add failed: %s', e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -158,7 +160,8 @@ export async function getGroupNoticeWebAPI(
|
|||||||
);
|
);
|
||||||
// console.log(JSON.stringify(ret, null, 2));
|
// console.log(JSON.stringify(ret, null, 2));
|
||||||
return ret?.ec === 0 ? ret : undefined;
|
return ret?.ec === 0 ? ret : undefined;
|
||||||
} catch {
|
} catch (e) {
|
||||||
|
log.warn('getGroupNoticeList failed: %s', e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,11 +218,15 @@ export async function uploadGroupNoticeImage(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
req.on('error', () => resolve(undefined));
|
req.on('error', (err) => {
|
||||||
|
log.warn('uploadGroupNoticeImage transport error: %s', err.message);
|
||||||
|
resolve(undefined);
|
||||||
|
});
|
||||||
req.write(body);
|
req.write(body);
|
||||||
req.end();
|
req.end();
|
||||||
});
|
});
|
||||||
} catch {
|
} catch (e) {
|
||||||
|
log.warn('uploadGroupNoticeImage failed: %s', e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,7 +267,8 @@ export async function deleteGroupNotice(
|
|||||||
false
|
false
|
||||||
);
|
);
|
||||||
return ret?.ec === 0;
|
return ret?.ec === 0;
|
||||||
} catch {
|
} catch (e) {
|
||||||
|
log.warn('deleteGroupNotice failed (group=%s fid=%s): %s', groupCode, fid, e instanceof Error ? (e.stack ?? e.message) : String(e));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,9 @@ export class OneBotInstance {
|
|||||||
this.apiHandler = new ApiHandler(buildApiContext(ctx), uinNum > 0 ? uinNum : undefined);
|
this.apiHandler = new ApiHandler(buildApiContext(ctx), uinNum > 0 ? uinNum : undefined);
|
||||||
this.networkManager = new OneBotNetworkManager();
|
this.networkManager = new OneBotNetworkManager();
|
||||||
this.installAdaptersFromConfig(config);
|
this.installAdaptersFromConfig(config);
|
||||||
void this.networkManager.openAll();
|
void this.networkManager.openAll().catch((err) => {
|
||||||
|
this.log.warn('openAll failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
|
|
||||||
this.startHeartbeat();
|
this.startHeartbeat();
|
||||||
this.rkeyCache.warmUp(this.bridge, this.uin);
|
this.rkeyCache.warmUp(this.bridge, this.uin);
|
||||||
@@ -104,7 +106,9 @@ export class OneBotInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reloadConfig(config: OneBotConfig): void {
|
reloadConfig(config: OneBotConfig): void {
|
||||||
void this.applyConfigDiff(config);
|
void this.applyConfigDiff(config).catch((err) => {
|
||||||
|
this.log.warn('applyConfigDiff failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
@@ -112,7 +116,9 @@ export class OneBotInstance {
|
|||||||
this.stopHeartbeat();
|
this.stopHeartbeat();
|
||||||
this.disposeEventPipeline?.();
|
this.disposeEventPipeline?.();
|
||||||
this.disposeEventPipeline = null;
|
this.disposeEventPipeline = null;
|
||||||
void this.networkManager.closeAll();
|
void this.networkManager.closeAll().catch((err) => {
|
||||||
|
this.log.warn('closeAll failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
this.messageStore.close();
|
this.messageStore.close();
|
||||||
this.mediaStore.close();
|
this.mediaStore.close();
|
||||||
}
|
}
|
||||||
@@ -141,8 +147,12 @@ export class OneBotInstance {
|
|||||||
this.cacheMessageEvent(event);
|
this.cacheMessageEvent(event);
|
||||||
this.logReceivedMessage(event);
|
this.logReceivedMessage(event);
|
||||||
// NetworkManager builds the dispatch payload once and fans out to every
|
// NetworkManager builds the dispatch payload once and fans out to every
|
||||||
// active adapter in parallel via Promise.allSettled.
|
// active adapter in parallel via Promise.allSettled — per-adapter errors
|
||||||
void this.networkManager.emitEvent(event);
|
// are already isolated, so the outer promise should never reject. The
|
||||||
|
// .catch is a belt-and-suspenders guard against future refactors.
|
||||||
|
void this.networkManager.emitEvent(event).catch((err) => {
|
||||||
|
this.log.warn('emitEvent failed: %s', err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildNetworkContext(): NetworkAdapterContext {
|
private buildNetworkContext(): NetworkAdapterContext {
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
import type { MessageElement } from '../bridge/events';
|
import type { MessageElement } from '../bridge/events';
|
||||||
import type { JsonValue, JsonObject } from './types';
|
import type { JsonValue, JsonObject } from './types';
|
||||||
|
import { createLogger } from '../utils/logger';
|
||||||
|
|
||||||
|
const log = createLogger('MsgParser');
|
||||||
|
|
||||||
export interface ParseMessageOptions {
|
export interface ParseMessageOptions {
|
||||||
resolveReplySequence?: (replyMessageId: number) => number | null;
|
resolveReplySequence?: (replyMessageId: number) => number | null;
|
||||||
@@ -271,7 +274,7 @@ async function segmentToElement(type: string, data: Record<string, unknown>, opt
|
|||||||
const musicJson = await resp.text();
|
const musicJson = await resp.text();
|
||||||
return { type: 'json', text: musicJson };
|
return { type: 'json', text: musicJson };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn(`[MsgParser] music sign failed: ${e}, falling back to local card`);
|
log.warn('music sign failed: %s, falling back to local card', e instanceof Error ? e.message : String(e));
|
||||||
// Fallback: build a basic card locally
|
// Fallback: build a basic card locally
|
||||||
const title = String(data.title ?? 'Music');
|
const title = String(data.title ?? 'Music');
|
||||||
const jsonData = JSON.stringify({
|
const jsonData = JSON.stringify({
|
||||||
|
|||||||
@@ -77,7 +77,9 @@ export class HttpPostAdapter extends IOneBotNetworkAdapter<HttpClientNetwork> {
|
|||||||
if (!this.isEnabled) return;
|
if (!this.isEnabled) return;
|
||||||
const json = pickDispatchJson(payload, this.options);
|
const json = pickDispatchJson(payload, this.options);
|
||||||
if (json === null) return;
|
if (json === null) return;
|
||||||
void this.postEvent(json, event);
|
void this.postEvent(json, event).catch((err) => {
|
||||||
|
this.log.warn('[%s] postEvent threw: %s', this.name, err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async postEvent(payload: string, event: JsonObject): Promise<void> {
|
private async postEvent(payload: string, event: JsonObject): Promise<void> {
|
||||||
|
|||||||
@@ -119,7 +119,9 @@ export class WsClientAdapter extends IOneBotNetworkAdapter<WsClientNetwork> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('message', (raw: Buffer) => {
|
socket.on('message', (raw: Buffer) => {
|
||||||
void this.handleApiMessage(socket, raw);
|
void this.handleApiMessage(socket, raw).catch((err) => {
|
||||||
|
this.log.warn('[%s] handleApiMessage threw: %s', this.name, err instanceof Error ? (err.stack ?? err.message) : String(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('close', () => {
|
socket.on('close', () => {
|
||||||
|
|||||||
Reference in new issue
Block a user