refactor(onebot): unify action-error retcode at the runAction seam
将「动作抛错返回什么错误码」从各处不一致(100 / 1200 / 漏包→1200)收敛到唯一 兜底处 ApiHandler.runAction,统一为 ACTION_FAILED(100) + error.message。 阶段 1(改 seam 本身): - runAction catch:默认码 1200 → 100;文案 'internal error' 兜底改为 String(error)。 OidbError.message 本就含 QQ 服务端 code,无需特判。 - 新增 3 个测试锁定策略:抛 Error→100+message;抛 OidbError→100 且 wording 含 code+server msg;抛非 Error→100+String()。 - rename_group_file 那个原断言 1200 的测试(未包裹动作、落 runAction)改为 100。 各 action 里多余的 try/catch(qzone/group-album 等仍返回 1200)留待后续阶段删除, 在此之前其失败不经此 seam。onebot 全量 516 全绿、typecheck 干净。
This commit is contained in:
@@ -178,15 +178,23 @@ export class ApiHandler {
|
||||
response = await handler(params, sink);
|
||||
this.log.trace(() => [`${action} ⇒ ${response.status} (${Date.now() - startedAt}ms)`]);
|
||||
} catch (error) {
|
||||
// Action failures are almost always param-shape problems coming
|
||||
// from the OneBot client; warn (not error) is the right level so
|
||||
// the log file stays a useful signal of real internal faults.
|
||||
// Single error seam: any throw from a handler (bridge/OIDB business
|
||||
// failure or an unexpected internal fault) maps here to one policy —
|
||||
// ACTION_FAILED (100) + the error's message. Action `run` bodies
|
||||
// shouldn't need to hand-roll their own try/catch → failedResponse:
|
||||
// doing so only produced inconsistent retcodes (100 vs 1200) for the same
|
||||
// kind of failure. The remaining per-action catches (qzone / group-album,
|
||||
// still returning 1200) are being removed in a follow-up phase — until
|
||||
// then their failures never reach this seam. `OidbError.message` already
|
||||
// carries the QQ server code, so no special-casing is needed. warn (not
|
||||
// error) keeps the log file a useful signal without drowning it in
|
||||
// expected client-side failures.
|
||||
this.log.warn('%s failed: %s\n%s',
|
||||
action,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
error instanceof Error ? (error.stack ?? '') : '');
|
||||
const message = error instanceof Error ? error.message : 'internal error';
|
||||
response = failedResponse(RETCODE.INTERNAL_ERROR, message);
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
response = failedResponse(RETCODE.ACTION_FAILED, message);
|
||||
}
|
||||
this.notifyObservers(action, params, response, Date.now() - startedAt);
|
||||
return response;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { ApiHandler } from '../src/api-handler';
|
||||
import { OidbError } from '@snowluma/protocol/oidb-service';
|
||||
import { subscribeLogs, type LogEntry } from '@snowluma/common/logger';
|
||||
import { summarizeParams } from '@snowluma/common/log-summary';
|
||||
|
||||
@@ -108,6 +109,35 @@ describe('ApiHandler dispatch logging', () => {
|
||||
expect(warn!.message).toMatch(/at\s+/);
|
||||
});
|
||||
|
||||
// The single error seam (ADR-0006 narrow seam): any throw from a handler
|
||||
// maps to ONE policy here — ACTION_FAILED (100) + the error message —
|
||||
// instead of each action hand-rolling its own inconsistent try/catch.
|
||||
it('maps any handler throw to ACTION_FAILED (100) with the error message', async () => {
|
||||
const handler = new ApiHandler(emptyContext(), 12345);
|
||||
handler.registerAction('kaboom', async () => { throw new Error('permission denied'); });
|
||||
|
||||
const result = await handler.handle('kaboom', {});
|
||||
expect(result).toMatchObject({ status: 'failed', retcode: 100, wording: 'permission denied' });
|
||||
});
|
||||
|
||||
it('surfaces the OidbError code + server message through error.message (no special-casing)', async () => {
|
||||
const handler = new ApiHandler(emptyContext(), 12345);
|
||||
handler.registerAction('oidb_boom', async () => { throw new OidbError(34, 'no permission', 0x11ec, 2); });
|
||||
|
||||
const result = await handler.handle('oidb_boom', {});
|
||||
expect(result).toMatchObject({ status: 'failed', retcode: 100 });
|
||||
expect((result as { wording: string }).wording).toContain('34');
|
||||
expect((result as { wording: string }).wording).toContain('no permission');
|
||||
});
|
||||
|
||||
it('renders a non-Error throw via String()', async () => {
|
||||
const handler = new ApiHandler(emptyContext(), 12345);
|
||||
handler.registerAction('weird', async () => { throw 'just a string'; });
|
||||
|
||||
const result = await handler.handle('weird', {});
|
||||
expect(result).toMatchObject({ status: 'failed', retcode: 100, wording: 'just a string' });
|
||||
});
|
||||
|
||||
it('logs unknown actions at debug level', async () => {
|
||||
const handler = new ApiHandler(emptyContext(), 99);
|
||||
await handler.handle('not_a_real_action', {});
|
||||
|
||||
@@ -730,13 +730,13 @@ describe('extended-actions / rename_group_file', () => {
|
||||
expect(rename).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('surfaces oidb errors (handler maps a thrown error to INTERNAL_ERROR, as its sibling file ops do)', async () => {
|
||||
it('surfaces oidb errors — a thrown error maps to the single seam (ACTION_FAILED 100 + message)', async () => {
|
||||
const rename = vi.fn(async () => { throw new Error('rename rejected'); });
|
||||
const bridge = fakeBridge({ apis: { groupFile: { rename } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('rename_group_file', {
|
||||
group_id: 1, file_id: '/a', current_parent_directory: '/', new_name: 'x',
|
||||
});
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 1200, wording: 'rename rejected' });
|
||||
expect(res).toMatchObject({ status: 'failed', retcode: 100, wording: 'rename rejected' });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user