It is pretty standard that when a Logic App fails, we want to get the exact error message describing why it failed. But, of course, we can always go to the Logic App run history – except if it is a Stateless workflow on Standard – to check where and why it failed. And obviously, we will have all the information necessary to solve the problem.
However, most of the time, if not always, we want to log the error in some place like Application Insights, SQL Database, Event Hubs, etc. To accomplish that, we need to access this error message in runtime.
As I explained in one of my Logic App Best practices, Tips and Tricks: Error handling… configure run after settings. To implement advanced Error handling capabilities like:
- Creating a try-catch statement consisting of a try block followed by one catch clause to handlers different exceptions that can occur on the try block.
- Or creating a try-catch-finally statement.
we have to use a combination of different Scopes.
The try-catch-finally statement handles all of the errors that may occur in a block of code. Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things.
- The try statement allows you to define a block of code to be tested for errors while it is being executed –> this will be your Actions inside Logic App that describe the logic of your business process.
- The catch statement allows you to define a block of code to be executed if an error occurs in the try block –> this will be your Actions inside Logic App that your want to run if some failure occurs in the standard actions of your business process.
- The finally statement lets you execute code after try and catch statements, regardless of the result (if there was a fail inside the try statement or if everything was completed successfully).
To implement a try-catch or try-catch-finally statements we need to:
- Add a Scope, we can call it Try-Scope, and add all the actions that you want to control errors inside this scope.
- Just immediately after the Try-Scope action, add a new Scope, we can call it Catch-Scope and add all the actions necessary to perform in case of errors, like rollback actions or logging actions.
- Now on the Catch-Scope action, click on the … (3 dots) and select Configure run after option
- Click on the Try-Scope to present the run-after options and select:
- has failed
- has timed out
- note: unselect the is successful option
- and click Done.
Now inside the catch scope, we will have an action logging the error but before I call it, I need to get the detailed error message. Something similar to the e.Message that we have on our C# code
To do this, we need to first filter the run action status result for Failed items. For that, the Logic App runtime has a Workflow function call result that is able to return the inputs and outputs from the top-level actions inside the specified scoped action, such as For_each
, Until
, and Scope
. So, inside of our Catch Scope add the following action:
- Inside the Catch scope, select Add an action.
- In the search box, enter Filter array, and from the result panel select the Data Operations, Filter array action
- And provide the following information:
- on the From property place the following expression
- result(‘Try_scope’)
- Note: that ‘Try_scope? is the name of the previous scope, so you need to adjust this value to your scenario
- on the condition, on the left textbox place the following expression
- item()?[‘status’]
- Note: this is always equal
- Leave the operator as is equal to and on the right textbox place the following value
- Failed
- on the From property place the following expression
Because this only brings the results of the top-level actions, what we want to grab is the first error that is reported there. To do that we need to for example set a variable with the error message present in the previous action output.
- Assuming that we already have a variable initialized. Select Add an action.
- In the search box, enter Variables, and from the result panel select the Variables, Set variable action.
- On the variable shape, select the variable we want to use and on the value set the following expression:
- first(body(‘Filter_array’))?[‘error’][‘message’]
his is a simple approach that you can use for simple and straightforward processes that don’t contain conditions or loops, something similar to this:
However, if you are creating more enterprise processes, they will be more complicated and we normally have nested conditions, conditions inside the loops, and so on. In that cases, this approach will not work. For example in this scenario:
We would expect, and we want to get the error where indeed happen – marked this the arrow on the picture. However, what we get is the top-level action description:
- ActionFailed. An action failed. No dependent actions succeeded.
Of course, this is not what intend for. Stay tuned because I will explain how to get the correct error message in the second part of this blog post.
1 thought on “How to get the Error Message with Logic App Try-Catch (Part I)”