diff --git a/packages/onebot/src/api-handler.ts b/packages/onebot/src/api-handler.ts index 743c393b..b512d0b4 100644 --- a/packages/onebot/src/api-handler.ts +++ b/packages/onebot/src/api-handler.ts @@ -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; diff --git a/packages/onebot/tests/api-handler-logging.test.ts b/packages/onebot/tests/api-handler-logging.test.ts index 72d3339a..5aef018f 100644 --- a/packages/onebot/tests/api-handler-logging.test.ts +++ b/packages/onebot/tests/api-handler-logging.test.ts @@ -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', {}); diff --git a/packages/onebot/tests/extended-actions.test.ts b/packages/onebot/tests/extended-actions.test.ts index 68e3594d..dc7c4ce0 100644 --- a/packages/onebot/tests/extended-actions.test.ts +++ b/packages/onebot/tests/extended-actions.test.ts @@ -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' }); }); });