How to get actual error message of Scope in Logic App | Exception Handling in Logic app

{tocify} $title={Table of Contents}


Introduction


No matter which technology/programming language you are using to build a solution, exception handling is an integral part.

And it should not be left unaddressed, as exceptions abnormally terminates the execution of the program.

An exception is an event(unplanned)  that occurs while a program is executing and disrupts the flow of its instructions, example - invalid input or a loss of connectivity etc.

Exception handling ensures that the flow of the program doesn't break even when an exception occurs.


In Logic app too, we have provisions to implement exception handling.

But unlike other programming languages where you have specific  exception objects in consideration when implementing exception handling, here broadly implemented as either of two
1. Connection Failure - Retry mechanism is used
2. All other exceptions - Run after property is used

Let's see how error scenario is handled and  implemented

Individual Action's Exception Handling in Logic App

Every action which we add in workflow is a candidate for an error/exception. 


Retry Policy in Logic App

A retry policy applies to intermittent failures, characterized as HTTP status codes 408, 429, and 5xx, in addition to any connectivity exceptions. 

Four variants/types are available to choose from

Retry policy in Logic App


If the retry policy definition is not specified explicitly, a fixed strategy is used with default retry count and interval values(The default is an exponential interval policy set to retry 4 times).

If you don't want to have retry, then to disable the retry policy, set its type to None.

So for any action which is trying to connect another application or is calling an api explicitly, you can configure retry policy

e.g.,

Go to settings of any connector like blob connector , sql connector , http connector etc. (open the ellipses menu (...), and select Settings)


Retry Policy in Logic app


Note, the retry interval is specified in the ISO 8601 format( P<period>T<time>).

The above interval says  02(period) and M(minutes), so retry will take place after an interval of 2 minutes.

Run After property in Logic App

Logic app is a JSON based DSL describing a dependency graph of actions and it is with the help Run after property available on each action (except trigger).

There are four status options available to use:
  • is successful
  • has timed out
  • is skipped
  • has failed

Except Trigger, all other Action(step) has dependency on some other action, and whenever you add new step in Logic app - Run after property is automatically set (on Successful Status of preceding step).

Run After property in Logic App




So, if you have to perform some action to handle failure/error/exception scenario, then you have to explicitly configure Run after property to look at Failed status/Timed Out of previous step. 



Configure run after property to run on failure



How to get error message of a failed action in Logic App

Capturing error is helpful, as it gives clarity about the error. Which can be further logged somewhere, or respective stakeholders can be notified by sending email with the error message.

Below is snap, where I am configuring an action to capture the error msg of an Action named Set Variable

1. Configure Run After property - select  has timed out and has failed status.

So this action will execute only if there is failure in Set Variable action

Configure Run after property for failed status


2. Capture the error msg, for that use following expression

                  
                            actions('Set_variable')['error']['message']



capture error message of an action in logic app

3. Test the flow with input which will fail and check the result. As can be seen error message is captured and assigned to a variable.

Based on requirement, you can log it or send email or take some other action.

error message captured in Logic app



It can be considered as Try..Catch block for individual Action. 


Multiple Action's Exception Handling in Logic App - Using Scope


Whenever there is a need to encompass multiple steps in any programming language we use Try block and to catch exceptions of any of the steps in Try block we add Catch block.


How do you catch exceptions in logic app?


In Logic app we make use of one of the  flow controls shape - Scope shape ( Logical grouping of actions).

When we use a scope to run set of actions (Multiple actions) and if any action in the scope fails or end unexpectedly, the scope is marked as "Failed" or "Aborted" respectively. 

Under the scope, add a condition that checks the scope's failed status - add another scope which checks if  status is Failed/Timed out i.e. configure Run after property of this scope to run on earlier scopes failed/timed out status. 


How to catch exceptions in logic app - Example

To showcase how it can be done, have created a simple recurrence based  logic app which has two scope -  Try and Catch.

After trigger and Initializing variables have been added couple of actions in the Try scope, following it is the Catch Exception scope in which I am sending email with the Error message.




Exception Handling Logic App

To capture error message have used following expression

actions('Try')['error']['message']


Testing

To make sure that there is an exception raised in Try scope, I configured incorrect url in HTTP action. And triggered the flow manually.

And as expected, an exception was raised in HTTP action and flow was passed on to Catch exception scope. Where an email was triggered with respective error message.

Following is the email received

Error notification mail with Error message

But note that we haven't received the actual error message. The message says

An action failed. No dependent actions succeeded.

This error message is quite generic and it tells that something has gone wrong in Try scope but does not tell the specific details as to what was the actual error.

So how do we get error of an action/step which failed in the scope?

How to get actual error message of Scope in Logic App



In order to get the actual error message, we will need to make use of 
  • Filter Array action from Data operations and 
  • result() function
In the same logic app created above, we add Filter array in Catch Exception scope and input to it would be output of Try scope (which is an array of output of each step executed in it) - result('Try') , with a condition to check for the items in this array having status as failed.

Filter array and result function in Catch exception scope



And in Send Email action replace 

            actions('Try')['error']['message']

with 

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


Note:  
The result() function returns the results only from the top-level actions and not from deeper nested actions such as switch or condition actions.



Testing

Tested again with the same incorrect url in http action, but this time received mail with actual error message (highlighted in yellow below)

actual error message of action in scope logic app

How exception handling  works in logic App

When we add Scope and actions within it, the output of the scope consists of output of each step/action and is stored as an Array. So if there are 10 actions in scope then 10 items will be added  (if all are succeeded) with a status as Success.

If there is failure in any of the action , then the status will be marked as Failed.

In the example in above step, we looked for the specific output which has status as failed from the array of output and for that we used Filter array.

Thus the output of Filter array consist only that item which has failed and in the succeeding action we used output of Filter array to get the actual error message.


Summary

In this article with the example we saw
  • What is exception handling in logic app
  • How to handle single action's exception using retry policy and Run after property
  • How to handle multiple action's exception using Scope and Run after property
  • How to get actual error message of Scope in Logic App using Filter Array and result Function
  • How to send email from Logic App




If you have questions or suggestions, feel free to do in comments section below !!!


Do share if you find this helpful .......
 
                          Knowledge Sharing is Caring !!!!!


Learn More about Logic App

 

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post