【问题标题】:Azure Function not throwing terminating errors with Stop ErrorAction in Push-OutputBindingAzure 函数不会在 Push-OutputBinding 中使用 Stop ErrorAction 引发终止错误
【发布时间】:2020-08-18 21:48:25
【问题描述】:

我正在尝试构建一个简单的 try-catch 语句,如下所示,但该 catch 块永远不会被执行

try {
  Push-OutputBinding -ErrorAction Stop -Name outputQueueItem -Value $Object
} 
catch {
  Write-Error "Catching Error"
  # code to catch the error
} 

问题是除了我有ErrorAction Stop 参数的事实之外,天蓝色函数没有抛出预期的错误作为终止。因此catch 代码永远不会被执行。

输出日志:

[8/18/2020 9:42:11 PM] INFORMATION: 00:00:09.9511800
[8/18/2020 9:42:11 PM] Executed 'Functions.Monitor' (Failed, Id=6bf398bd-de2b-460f-a44b-db9838feff3f)
[8/18/2020 9:42:11 PM] System.Private.CoreLib: Exception while executing function: Functions.Monitor. Microsoft.WindowsAzure.Storage: Messages cannot be larger than 65536 bytes.

错误是意料之中的,但是,catch 代码块从未被执行。

运行时信息:

Azure Functions Core Tools (3.0.2630 Commit hash: beec61496e1c5de8aa4ba38d1884f7b48233a7ab)
Function Runtime Version: 3.0.13901.0

我也尝试过更改变量$ErrorActionPreference = "Stop",但也没有用。

【问题讨论】:

  • 如果$ErrorActionPreference = 'Stop' 那么Write-Error "Catching Error" 将中止catch 块的执行,请避免从其中抛出错误(至少在您完成错误处理之前)跨度>
  • @MathiasR.Jessen 顺便说一句,您的预订-com 职业链接已失效;)
  • @MathiasR.Jessen 感谢您的贡献,我不知道那个。但是,在上面共享的示例代码中,不应该发生 ErrorAction Stop 它只是在命令级别,并且 $ErrorActionPreference 是 continue 并且不会在 catch 中打印错误
  • @ErickGuillen 正确,它不应该对 Continue 作为首选项变量产生影响 - 但我怀疑 Azure Function 脚本运行器可能会对发出的错误做出反应并中止执行?这是猜测,但也许值得一试?
  • @MathiasR.Jessen 我刚刚试了一下。即使使用$ErrorActionPreference = 'Stop',它也会在 try 函数中生成错误代码后继续执行,并且永远不会触发 catch。我认为 Azure Function Runner 可能有些奇怪

标签: azure powershell azure-functions


【解决方案1】:

您的脚本无法捕获此异常,因为它发生在脚本完成之后Push-OutputBinding 命令不会直接将数据写入 Azure 存储。相反,它只是将其存储在内部数据结构中,并在函数完成后实际写入 Azure 存储。

另请参阅:https://github.com/Azure/azure-functions-powershell-worker/issues/284

解决方法是将脚本更改为直接写入 Azure 存储,而不是调用 Push-OutputBinding,这样您就可以实现自己的错误处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 2015-02-13
    • 2020-09-26
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多