Power Community

Power Community

Azure Logic Apps: retrieving the exception details of an action group in a flow

In Azure Logic Apps (and in Power Automate too) to execute actions only if another group of actions succeed or fail, you can group those actions inside a scope. This structure is useful when you want to organize actions as a logical group, evaluate that group’s status and perform actions that are based on the scope’s status. After all the actions in a scope finish running, the scope also gets its own status.

Using the scope action is a must when creating complex workflows and scopes permits you to implement error handling in cloud workflows (something like a Try-Catch method in C#). But how can you implement a good error handling with scopes?

When I have a cloud workflow, I love grouping actions together and when an action fails, I often want to retrieve the error detail and log it or send details about the exception to other systems (email message, Teams message, Application Insights logging and more).

How to do that?

Here is an example that you can start using in your cloud-based workflows for improving your exception handling and logging.

In the following sample, I’ve created an Azure Logic Apps workflow that sends an HTTP request to an external endpoint. If the request succeeded, the response message and the HTTP status is retrieved and then other actions will be executed (omitted here). All these steps are defined inside the same scope.

If an action on this main scope fails, I want to retrieve the action that fails, the exception thrown, save the error details and then doing things (new actions) with those details.

As said before, to define this pattern of “exception handling” I’m using scopes. Here I have a main scope called Try and then a second scope called Catch.:

In the Catch scope I configure the run after setting as follows:

The Catch block will be executed only if the Try block fails, is skipped or has a time out.

In the Try block, I’m doing an HTTP call to an external endpoint that doesn’t exist, just to have an exception. The scope block is defined as follows:

When the Try scope fails, it send to the Catch block a JSON body (JSON array) with details of the actions status inside that scope. In my sample it’s something like the following:

[
  {
    "name": "HTTP",
    "inputs": {
      "uri": "http://abcdef",
      "method": "GET",
      "retryPolicy": {
        "type": "None"
      }
    },
    "startTime": "2023-02-28T13:46:36.0548298Z",
    "endTime": "2023-02-28T13:46:38.3518914Z",
    "trackingId": "xxx",
    "clientTrackingId": "xxx",
    "code": "UnresolvableHostName",
    "status": "Failed",
    "error": {
      "code": "UnresolvableHostName",
      "message": "Http request failed with status code 'NameResolutionFailure' and status message: 'The remote name could not be resolved: 'abcdef''."
    }
  },
  {
    "name": "Set_variable",
    "startTime": "2023-02-28T13:46:38.3673673Z",
    "endTime": "2023-02-28T13:46:38.3673673Z",
    "trackingId": "32b06f3c-2b7d-4727-a8b4-0edee1014cbe",
    "clientTrackingId": "xxxx",
    "code": "ActionSkipped",
    "status": "Skipped",
    "error": {
      "code": "ActionConditionFailed",
      "message": "The execution of template action 'Set_variable' is skipped: the 'runAfter' condition for action 'HTTP' is not satisfied. Expected status values 'Succeeded' and actual value 'Failed'."
    }
  }
]

To retrieve the exception details, in the Catch block I’m using the Filter array action to retrieve the message of the exception returned by the Scope action. Here I need to filter the JSON array items in order to retrieve only the item where status = Failed (this is the action that causes the exception):

This is how I’ve defined the actions in the Catch block:

Here is the most tricky part. How can I define the formulas in the above actions to retrieve the desired details?

 In the Filter array action the input must be the output of the Try scope, so the formula to add is the following:

Then, from the JSON array returned from the From field (and that you can see above), we need to filter only the items where status = failed, so we can write the following in the filter field:

item()?['status']

Now that we have the result of the failed action, we can retrieve the details of the exception by using the following expression:

body('Filter_array')?[0]?['error']?['message']

The expression searches for the message tag under the error block of the returned JSON:

The returned exception message is here assigned to a variable called Exception. From here you have the exception detail and you can act as you want (email message, Teams message, custom loggin etc.).

What happens if I execute this workflow? The HTTP action inside the Try block fails and the Catch block is executed:

The Filter array action receives as input the JSON body of the actions from the first scope and returns as output the filtered JSON (details of only the action that is failed):

In the last action, the message detail of the error is retrieved and it’s assigned to the exception variable for further processing:

{
    "name": "HTTP",
    "inputs": {
      "uri": "http://abcdef",
      "method": "GET",
      "retryPolicy": {
        "type": "None"
      }
    },
    "startTime": "2023-02-28T13:21:33.585643Z",
    "endTime": "2023-02-28T13:21:35.866918Z",
    "trackingId": "xxx",
    "clientTrackingId": "xxx",
    "code": "UnresolvableHostName",
    "status": "Failed",
    "error": {
      "code": "UnresolvableHostName",
      "message": "Http request failed with status code 'NameResolutionFailure' and status message: 'The remote name could not be resolved: 'abcdef''."
    }

You’ve now implemented a great exception handling in your serverless workflow. I suggest to start using this pattern if you have complex workflows that you need to monitor and handle exception handling and logging.

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

How to transform work with plugins for Microsoft 365 Copilot and AI apps

With Microsoft 365 Copilot, people now have access to a next-gen AI assistant that can alleviate digital debt while...

More Articles Like This

- Advertisement -spot_img