使用 javascript 在 azure 函数中处理服务总线错误消息

Handling service bus error messages in azure function using javascript

我有一个使用服务总线主题触发器的 azure 函数,我想优雅地处理错误消息,我希望能够放弃消息并将异常传递给它,这样我就可以在 属性 当我读取死信队列时。

这是我的代码:

const serviceBusTopicTrigger: AzureFunction = async function(context: Context, mySbMsg: any): Promise<void> {
    // do something messy that may fail.
};

当我的函数因异常而失败时,消息会按预期发送到 DLQ,但问题是,它不会保存抛出的异常,它只会告诉您它已尝试执行该方法 10 次,并且不能。

我希望它能够捕获异常并将其添加到消息属性中,这样当我处理 DLQ 队列时我将能够知道错误的原因。更重要的是,由于代码因异常而失败,我希望它从第一次运行消息时就放弃,因此它不必重试 10 次。

我在想这样的事情:

const serviceBusTopicTrigger: AzureFunction = async function(context: Context, mySbMsg: any): Promise<void> {
    try{
        // do something messy and that may fail
    }
    catch(error){
        context.bindingData.userProperties['DeadLetterReason'] = 'Internal server error';
        context.bindingData.userProperties['DeadLetterErrorDescription'] = JSON.stringify(error);
        context.bindingData.abandonMsg();
    } 
};

我还没有找到任何关于此类内容的文档,所以有可能吗?或者我可以强制将消息发送到 DLQ 吗?像这样:

const serviceBusTopicTrigger: AzureFunction = async function(context: Context, mySbMsg: any): Promise<void> {
    try{
        // do something messy and that may fail
    }
    catch(error){
        context.bindings.deadLetterQueue.userProperties['DeadLetterReason'] = 'Internal server error';
        context.bindings.deadLetterQueue.userProperties['DeadLetterErrorDescription'] = JSON.stringify(error);
        context.bindings.deadLetterQueue = mySbMsg;
    } 
};

或者最后,遗憾的是,我是否必须直接从方法中管理我的错误,并可能将其直接从那里发送到 azure 存储 table 或队列以通知错误?,我不喜欢那样,因为然后我将处理死信队列中的错误以及我在不同位置的函数中的错误。这是唯一的方法吗?

还有什么想法吗?

谢谢。

首先我觉得nodejs做不到这一点。 nodejs在C#中丢失了类型信息,应该是这样发送的:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.deadletterasync?view=azure-dotnet

When my function fails with an exception the message goes to the DLQ as expected, but the problem is, that it doesn't save the exception thrown, it only tells you that it tried to execute the method 10 times and it couldn't.

创建服务总线主题的订阅时,Max Delivery Count 默认设置为 10。默认值为10,最小可以设置为1。

当你开启服务总线信息自动补全时,如下图:

host.json

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "autoComplete": true
      }
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 3.1.0)"
  }
}

当时发生错误,函数运行失败,调用abandon方法,Delivery Count +1,同时发回消息。

那么一旦你用try-catch,你根本不会重试10次。该函数被视为运行成功,function 运行 success 消息消失,不发回。(即一次try-catch只会执行一次。)

I haven't been able to find any documentation about something like this, so is it possible? or can I force the message to the DLQ?

不,这是不可能的。缺少相关类型,无法通过 JavaScript.

手动发送到死信

Or finally and sadly do I have to manage my error directly from the method and maybe send it directly from there to an azure storage table or queue to notify errors?, I wouldn't like that because then I would be handling, both errors from the dead letter queue, and from my functions in different places. Is this the only way?

为什么说死信中仍然出现错误呢?在自动完成的情况下使用try-catch,应该不会发送死信。