question

PoddarAnkur-1290 avatar image
0 Votes"
PoddarAnkur-1290 asked MartinJaffer-MSFT commented

Capturing Runbook error in ADF through WebHooks

I am trying to capture the error and Fail the webhook activity if my runbook is failed.
I have tried with callback URL but it is not working.
Also webhook activity is getting successful even the runbook got failed.

Runbook code is written in PowerShell.

ErrorMessage is capturing the error from catch statement.

code

callbackurl

if ($ErrorMessage) {
$OutputJson = @"
{
"output": { "message": "$ErrorMessage" },
"statusCode": 500,
"error": {
"ErrorCode": "Error",
"Message": "$ErrorMessage"
}
}
"@
}

if ($callBackUri) {
Invoke-WebRequest -Uri $callbackuri -UseBasicParsing -Method POST -ContentType "application/json" -Body $OutputJson
}

azure-data-factoryazure-automation
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @PoddarAnkur-1290 and welcome to Microsoft Q&A.
I can understand your frusteration. I mostly work on Data Factory, and this is my first time working on Automation Runbook.
In my attempts to repro (successfully, I think?) , Iran into needing to declare the $WebhookData in the runbook script. While I am following the documentation on this, something seems off as I try to parse the RequestBody. There is also the need to create/renew the Webhook associated with the Runbook.

Not working yet, but this is my code so far:

 Param
 (
     [object]$WebhookData
 )
 write-output $WebhookData
 write-output ($WebhookData.RequestBody)
 write-output (ConvertTo-Json($WebhookData.RequestBody))
 write-output (ConvertTo-Json( $WebhookData.RequestBody))
    
 $ErrorMessage = "IBrokeThis"
 $callBackUri = ConvertTo-Json( $WebhookData.RequestBody).callBackUri
    
 if ($ErrorMessage) {
 $OutputJson = @"
 {
 "output": { "message": "$ErrorMessage" },
 "statusCode": 500,
 "error": {
 "ErrorCode": "Error",
 "Message": "$ErrorMessage"
 }
 }
 "@
 }
 write-output $ErrorMessage
 write-output $callBackUri
 if ($callBackUri) {
 Invoke-WebRequest -Uri $callBackUri -UseBasicParsing -Method POST -ContentType "application/json" -Body $OutputJson
 }



0 Votes 0 ·

1 Answer

PoddarAnkur-1290 avatar image
0 Votes"
PoddarAnkur-1290 answered MartinJaffer-MSFT commented

Thanks @MartinJaffer-MSFT for looking and helping me out.

Now i am able to send back the error details using CallbackURL to webhook activity in ADF and if there is any failure in runbook the webhook activity will also failed.

I am using Try and catch block in powershell script.

PFA code which worked for me:-

   param
      (
          # Get webhook data via webhook
          [Parameter(Mandatory=$False,Position=1)]
          [object] $WebhookData
       )
     $Parameters = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
     $AzureId = $Parameters.AZUREID                                                                           
     $callBackUri = $Parameters.callBackUri
     $context ='Blob'
     $ContainerName = 'ABC'

     try
     {
         Write-Output "INFO: Uploading file to Blob container"
         Set-AzStorageBlobContent -File "file_$($today).csv" -Context $context -Container $ContainerName                                                                                                                                                        
         if($? -eq $False){
             Write-Error -Message 
         }                
         Write-Output "INFO: Report has been uploaded and is now available on blob storage"
         Invoke-WebRequest -Uri $callbackuri -UseBasicParsing -Method POST
       } catch {                    
  $jsonRequest = [ordered]@{
      output= @{
          restoreStatus = "Failed"
          }
      error = @{
          ErrorCode = "RestoreError"
          Message = "Runbook Error: $Error[0]"
          }
      statusCode = "500"
      }

       $jsonBody = $jsonRequest | ConvertTo-Json -Depth 10

      $jsonBody
      $headers = @{
          "Content-Type"="application/json"
     }
     Invoke-WebRequest -Uri $callbackuri -UseBasicParsing -Method POST -Body $jsonBody -Header $headers

  Write-Output "Error: Unable to upload report:"
    }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for sharing the solution @PoddarAnkur-1290 . I tried many combinations to extract the callBackUri, but kept failing.

I think the part I was missing was the -InputObject in $Parameters = (ConvertFrom-Json -InputObject $WebhookData.RequestBody).

Now my mind can be at peace.

1 Vote 1 ·