Error Handling – Power Automate

yellow scrabble tiles

You have multiple flows, they run multiple times a day and then one starts failing. Power Automate will eventually notify you that your flow has failed a few times recently and that you should take a look but how about a proactive approach to error handling? If your flow fails you could get a link directly into the history of that flow via email or a flowbot teams message.

Firstly, you need to group your main steps of a flow into a Scope control. There are various advantages of a scope. It makes your flow appear more compact, can make it easier to navigate and understand, and most importantly means you can use the result() expression to obtain the history of an action. In my example below my main steps are in the try scope with the magic all happening in the catch scope. Note that the result expression can only return first level actions but can also be used to retrieve the results of actions in an apply to each. Watch my video to see how this might be possible.

Example try catch scopes Power Automate

The important thing to note is that the catch scope is configured to run after the main try scope has failed, otherwise it isn’t called into action. I also use the terminate action in the catch to indicate that the flow was cancelled if any of the actions in the try failed. This is so the results of the catch scope are easily recognisible in the traditional history of the flow as “cancelled”.

Catch scope runs on failed, skipped and time out

The catch scope will filter the function result(try) of the try scope where the status is equal to failed. We can then select from the body of the filter array the error code item()?[‘error’]?[‘code’] and the error message item()?[‘error’]?[‘message’] specific to the actual step that failed in the above try scope, whether it be the get file, update row or send email. This select is then nicely formatted into an HTML table for the email confirmation.

Note that you might consider using Coalesce for the error code as I noted that there were two potential codes to retrieve coalesce(item()?[‘error’]?[‘code’], item()?[‘code’])

Catch Scope, filter, select, html table and create item.

In my video I save each failure to a Microsoft List but you could use Dataverse. This allows me to create my own permanent history of flow runs. I can monitor trends and see how often a flow fails. The list also gives me a direct link to each flow history run which can make it easier to track down a particular failure. I have created bespoke views to group by flow names and then error code.

Microsoft List with Flow Error History

Some of the expressions used in the create item were based on the first object of the filter array as follows:

Title workflow()?[‘tags/flowDisplayName’]

Action Name First(body(‘Filter_array’))?[‘name’]

Error Code coalesce(First(body(‘Filter_array’))?[‘error’]?[‘code’], First(body(‘Filter_array’))?[‘code’])

Error Message First(body(‘Filter_array’))?[‘error’]?[‘message’]

The final piece of magic is the URL for the actual history run of the flow. This takes you direct into the history and allows you to immediately start working out what went wrong. Remember that your email or teams message has already alerted you to what the error message was and for what step! Official Flow history can only be accessed for 28 days.

Send an email of Teams Message regarding the flow history

In order to create the unique URL for the flow history the following string is composed. Note that the start of the URL may vary depending on where your environment is located around the world.

concat(‘https://unitedkingdom.flow.microsoft.com/manage/environments/’,workflow()?[‘tags’]?[‘environmentName’],’/flows/’,workflow()?[‘name’],’/runs/’,workflow()?[‘run’][‘name’])

And that is it. Your flow fails and you know exactly which run and why and can get straight to work putting it right. Don’t forget that you can copy the scope for catch and re-use it in future flows. Make sure you place your flow actions into a scope called try and copy the catch scope into your new flow. If you’re not sure how to do this, make sure you watch my video and see how!

Share