我有一个Azure功能设置。其触发器设置为服务总线队列消息。触发功能后,将以某种方式处理消息。如果在处理过程中遇到任何异常,我将处理该错误,并将消息保存在Azure表存储集中作为输出绑定,以进行记录。
我想处理在向输出绑定中写入内容时引发的所有错误。我将如何去做?
错误详情
我在写入输出绑定(表存储)期间遇到的错误之一是某些键的值无法保存。例如,消息中的键之一是numbertime类型的值。在写入此数据时,我得到了一个例外,即键的值不能容纳Int32数字。我认为表存储尝试将数字转换为Int32默认值,但失败了。要解决此问题,我将所有键都转换为具有字符串值。现在我可以保存。
但是我仍然想在写入表存储输出绑定时处理任何不可预见的错误。
在这种情况下,处理错误很重要,因为如果未正确使用服务总线消息,则不会在服务总线队列中删除该消息,并且在完成Azure函数执行后,该消息再次触发函数,并且函数进入不确定循环。
以下是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();
}
侃侃无极
相关分类