Search code examples
javascriptazure-functionsazure-servicebus-subscriptions

Javascript : to explicitly send a message to dead letter queue in azure function app which is service bus triggered


I have service bus triggered azure function app. In the function app based on some condition i want to transfer the message to dead letter queue.

const sbClient = new ServiceBusClient(connectionString);
    const receiver = sbClient.createReceiver('chattopic', 'test', {
        receiveMode: "receiveAndDelete"
    });
    console.log(serviceBusMessage);
   await receiver.deadLetterMessage(serviceBusMessage, {
        deadLetterReason: "Error",
        deadLetterErrorDescription: "Error."
    })

However i get the following error

Result: Failure
Exception: The operation is not supported in 'receiveAndDelete' receive mode.
Stack: Error: The operation is not supported in 'receiveAndDelete' receive mode.

I get same error even when i change the receiveMode to peekLock. Can anyone please help here.


Solution

    • I think the reason you get this exception is because you are trying to dead letter the messages using the servicebusClient but those messages weren't fetched using that service bus client.

    • So, to remove this exception we would need the same client used by the service bus trigger to fetch the messages, but that is not available.

    • Also, you don't even need to create a service bus client, receiver etc. Even throwing an exception will dead letter the message.

    module.exports = async  function(context, mySbMsg) {
    throw new Error('Exception');
    }
    

    Before sending the message: enter image description here

    After sending the message: enter image description here

    Local terminal : enter image description here