From ec4ea4626ef9d97bbbab200778c9f78fc7389497 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Wed, 18 Feb 2026 15:39:54 +0100 Subject: [PATCH] fix: Respect error serialization. Signed-off-by: Paolo Insogna --- src/consumer.ts | 18 ++++++++++++------ test/queue.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/consumer.ts b/src/consumer.ts index 9f59c08..7d077b9 100644 --- a/src/consumer.ts +++ b/src/consumer.ts @@ -25,6 +25,8 @@ interface ConsumerEvents { requeued: [id: string] } +type ExtendedError = Error & { code?: string; toJSON?: () => Record } + /** * Consumer handles processing jobs from the queue */ @@ -209,7 +211,7 @@ export class Consumer extends EventEmitter extends EventEmitter { assert.strictEqual(status.state, 'completed') assert.deepStrictEqual(status.result, { result: 42 }) }) + + it('should use error.toJSON() for failed job status when available', async () => { + class JsonError extends Error { + toJSON (): unknown { + return { + message: this.message, + code: 'CUSTOM_ERROR', + details: { source: 'toJSON' } + } + } + } + + queue.execute(async () => { + throw new JsonError('Serialized by toJSON') + }) + + await queue.start() + + const failedPromise = once(queue, 'failed') + await queue.enqueue('job-1', { value: 21 }, { maxAttempts: 1 }) + await failedPromise + + const status = await queue.getStatus('job-1') + assert.ok(status) + assert.strictEqual(status.state, 'failed') + assert.deepStrictEqual(status.error, { + message: 'Serialized by toJSON', + code: 'CUSTOM_ERROR', + details: { source: 'toJSON' } + }) + }) }) describe('concurrency', () => {