Azure Function not throwing terminating errors with Stop ErrorAction in Push-OutputBinding

Viewed 545

I'm trying to build a simple try-catch statement like the following, but that catch block never gets executed

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

The problem is that the azure function is not throwing the expected error as terminating besides the fact I had the ErrorAction Stop parameter. Therefore the catch code never gets executed.

Output Log:

[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.

The error was expected, however, the catch code block never got executed.

Runtime Information:

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

I have also tried to change the variable $ErrorActionPreference = "Stop" but it didn't work either.

1 Answers

Your script can't catch this exception because it happens after the script is completed. The Push-OutputBinding command does not directly write data to Azure Storage. Instead, it just stores it in an internal data structure, and the actual writing to Azure Storage happens after the function completes.

See also: https://github.com/Azure/azure-functions-powershell-worker/issues/284

The workaround is to change your script to write to Azure Storage directly instead of invoking Push-OutputBinding, so you can implement your own error handling.

Related