通过 WebHooks 捕获 ADF 中的 Runbook 错误

Capturing Runbook error in ADF through WebHooks

Runbook 中的 powershell 脚本正在混合工作器上执行,当找不到文件时出错。但是,当通过 ADF webhook 调用时,activity 会通过。谁能告诉我如何捕获 ADF 中的错误?

脚本:

param(
      [Parameter (Mandatory = $false)]
      [object] $WebhookData
  )
Import-Module Az.Storage -force

  if($WebhookData){
      $parameters=(ConvertFrom-Json -InputObject $WebhookData.RequestBody) 
      if($parameters.callBackUri) {$callBackUri=$parameters.callBackUri}
  }
  
TRY
{
$connectionName = ***************
$storageAccountName = *****************
$sasToken = ************************

$context = New-AzStorageContext -StorageAccountName $storageAccountName -SASToken $sasToken

$localFile = "C:\VenaIntegrate\Data\In\"
$filename = "exporthierarchy" + (Get-Date -format "yyyyMMdd") + ".psv"
$filename2 = "exportattributes" + (Get-Date -format "yyyyMMdd") + ".psv"
$path = $localFile + $filename
$path2 = $localFile + $filename2

$filesystemName = ******

Set-AzStorageBlobContent -File $path -Container $filesystemName -Context $context -Force
Set-AzStorageBlobContent -File $path2 -Container $filesystemName -Context $context -Force
}

 CATCH {
            Write-Error "Error Occured"
            $callBackUri = $parameters.callBackUri
            # Create an error message
            # Message and statuscode will show up in ADF
            $body = [ordered]@{
            error = @{
                ErrorCode = "Error Occured"
                Message = "File not found"
            }
            statusCode = "404"
            }
 
            # Convert the string into a real JSON-formatted string
            $bodyJson = $body | ConvertTo-Json
 
            # Call back with error message in body and a JSON contenttype
            Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
#$callBackUri = $parameters.callBackUri
If (!$callBackUri)
{
    # Create an error message
    # Message and statuscode will show up in ADF
    $body = [ordered]@{
    error = @{
    ErrorCode = "ParameterError"
    Message = "Required parameters where not provided"
    }
    statusCode = "404"
    }
 
    # Convert the string into a real JSON-formatted string
    $bodyJson = $body | ConvertTo-Json
 
    # Call back with error message in body and a JSON contenttype
    Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}
else
{
    $body = [ordered]@{
    success = @{
    SuccessCode = "Process success"
    Message = "Process completed successfully"
    }
    }
 
    # Convert the string into a real JSON-formatted string
    $bodyJson = $body | ConvertTo-Json
 
    # Call back with error message in body and a JSON contenttype
    Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}

即使 runbook 在自动化帐户中失败,但在通过 runbook 调用时它在 ADF 中成功说 “没有可用的输出。您可以通过在回调的请求正文中设置输出 属性 来指定 webhook activity 输出”

您需要从命令“Set-AzStorageBlobContent”捕获 return。例如

$is_uploaded_path1 = Set-AzStorageBlobContent -File $path -Container $filesystemName -Context $context -Force

$is_uploaded_path2 = Set-AzStorageBlobContent -File $path2 -Container $filesystemName -Context $context -Force

现在你可以在 catch 块中做这样的事情了

If(!$is_uploaded_path1)
{
Invoke-WebRequest -UseBasicParsing -Uri $callBackUri -Method Post -Body $bodyJson -ContentType "application/json"
}

请注意,您还可以修改带有特定文件名的错误。