Originally published at warrenops.io .
In Node the failure modes of a RabbitMQ consumer are quieter than elsewhere. An async handler that throws does not crash anything: the promise rejects, nobody awaits it, and the message stays unacknowledged until the channel closes. NestJS, with its defaults, acknowledges the message before your handler runs, so a failure simply loses it. And a nack with requeue puts the message straight back at the head of the queue. Here is how to make failed messages land in a dead-letter queue with amqplib and with NestJS, and how to send them back once the bug is fixed.
amqplib: catch it, then nack without requeue
const amqp = require ( 'amqplib' ) ;
const conn = await amqp . connect ( process . env . AMQP_URL ) ; const ch = await conn . createChannel ( ) ;
// the dead-letter route await ch . assertExchange ( 'orders.dlx' , 'direct' , { durable : true } ) ; await ch . assertQueue ( 'orders.dlq' , { durable : true } ) ; await ch .…