【发布时间】:2019-04-08 05:39:42
【问题描述】:
我有一个 Azure 函数设置。它的触发器设置为 Service Bus Queue 消息。当 Function 被触发时,消息会以某种方式进行处理。如果在处理过程中遇到任何异常,我会处理该错误并将消息保存在 Azure 表存储集中作为输出绑定以用于日志记录。
我想处理向输出绑定写入内容时抛出的任何错误。我该怎么做呢?
错误详情
我在写入输出绑定(表存储)时遇到的错误之一是无法保存某些键的值。例如消息中的一个键是time,其值类型为number。在写入此数据时,我遇到了异常,即密钥的值无法放入 Int32 数字中。我认为表存储默认尝试将数字转换为Int32,但失败了。为了解决这个问题,我将所有键都转换为字符串值。现在我可以保存了。
但我仍然想在写入表存储输出绑定时处理任何不可预见的错误。
在这种情况下处理错误很重要,因为如果没有正确消费Service Bus Message,那么Service Bus Queue中的消息不会被删除,并且在Azure Function执行完成后,该消息会再次触发Function,并且函数进入无限循环。
以下是 Azure 函数的示例
module.exports = async function (context, mySbMsg) {
try{
// process service bus message
await processMsg(mySbMsg);
} catch(e) {
// if processing fails, save the message in Azure table
try{
// my own try to handle errors, but was unsuccessful
await new Promise(function (resolve, reject) {
context.bindings.tableBinding = [];
context.bindings.tableBinding.push({
PartitionKey: mySbMsg.id|| "unknown",
RowKey: time + "",
errorMessage: e.message || "",
...mySbMsg
})
resolve();
});
} catch (e) {
// do something HERE;
// exception on output binding didn't brought me here
}
}
context.done();
}
【问题讨论】:
标签: javascript azure azure-functions