diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js index f4a31a1a7e..e58715db03 100644 --- a/ds-bot/src/host/turns.js +++ b/ds-bot/src/host/turns.js @@ -28,6 +28,9 @@ export function turnFailure(error) { message, ...(typeof error?.status === 'number' ? { status: error.status } : {}), ...(typeof error?.requestId === 'string' ? { requestId: error.requestId } : {}), + // Kept so a turn failure can name its own line in the diagnostics log. A provider + // error has no stack worth keeping, and AUTH already dropped its message. + ...(code !== 'AUTH' && typeof error?.stack === 'string' ? { stack: error.stack } : {}), } } @@ -571,6 +574,7 @@ export function install(rt) { code: failure.code, status: failure.status, requestId: failure.requestId, + stack: failure.stack, bot: rt.botOf(rt.selfOf(id))?.name ?? (rt.roomOf(id) ? undefined : 'unknown Bot'), where: placeOf(id), sessionId: id, diff --git a/ds-bot/test/diagnostics.test.js b/ds-bot/test/diagnostics.test.js index 74f4053376..5724818e18 100644 --- a/ds-bot/test/diagnostics.test.js +++ b/ds-bot/test/diagnostics.test.js @@ -203,6 +203,11 @@ test('turnFailure drops AUTH messages and keeps the facts', () => { assert.deepEqual(turnFailure({ code: 'RATE_LIMIT', message: ' slow down ', requestId: 'r1' }), { code: 'RATE_LIMIT', message: 'slow down', requestId: 'r1' }) assert.deepEqual(turnFailure(undefined), { code: 'UNKNOWN', message: '' }) assert.equal(failureLine({ code: 'SERVER', status: 503, message: 'busy' }), 'SERVER 503: busy') + // The stack is what names the failing line in the log, and an AUTH failure keeps none. + const thrown = new Error('boom') + assert.equal(turnFailure(thrown).stack, thrown.stack) + assert.equal(turnFailure({ code: 'AUTH', message: '', stack: thrown.stack }).stack, undefined) + assert.equal(turnFailure({ code: 'SERVER', message: '', stack: 42 }).stack, undefined) }) function turnsHost() { @@ -247,6 +252,18 @@ test('a failed reply to message_bot tells the sender instead of leaving it waiti assert.deepEqual(noted.map(entry => [entry.source, entry.code, entry.bot, entry.where]), [['turn', 'SERVER', 'Writer', 'own chat']]) }) +test('a failed turn carries its stack into the diagnostics entry', () => { + const { emit, noted } = turnsHost() + const thrown = new Error('the line that failed') + emit('b', 'turn/start', { turn: 1 }) + emit('b', 'turn/end', { turn: 1, reason: { kind: 'error', error: { code: 'SERVER', message: 'upstream busy', stack: thrown.stack } } }) + assert.equal(noted.length, 1) + assert.match(noted[0].stack, /diagnostics\.test\.js/) + emit('b', 'turn/start', { turn: 2 }) + emit('b', 'turn/end', { turn: 2, reason: { kind: 'error', error: { code: 'AUTH', message: '', stack: thrown.stack } } }) + assert.equal(noted[1].stack, undefined) +}) + test('a failed group turn fails its waiter; a stopped one counts as no answer', () => { const { rt, emit } = turnsHost() rt.roomWaiters.set('r1', { resolve: () => assert.fail('resolved'), fail: failure => rt.roomWaiters.failed = failure })