Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ds-bot/src/host/turns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
}
}

Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions ds-bot/test/diagnostics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 })
Expand Down
Loading