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.
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”.
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’])
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.
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.
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!
Thanks you very much. It helped me a lot. Important with the Scope .. Thanks again.
Hi, great video about error handling!
I am experiencing an issue when it comes to getting the error message of a compose action that is within an Apply to each that is within a scope i.e. the example you showed at 16:20 . So basically I have a Scope, in it I have Apply To Each and in it a Compose action. The output of the Filter Array action using the expression results(Apply_To_Each) does not return an error in the json output (i.e. the error tag in the JSON is non-existent in the output) and hence error/message in the Select action is null. Any ideas why would something like this happen? In the Compose action itself I can see the full error (InvalidTemplate. Unable to process template language expressions in action ‘Compose2’….).
Thanks in advance!
Hi, results expression is limited to the top level scope/apply to each, if you want error level within an apply to each, you need to get results for that also, and potentially create a union on the results from the scope and apply to each.
Hi. Thanks for this very clear explanation of something I’ve been meaning to implement for all my flows for a long time. I think I’ve followed your instructions correctly, but when I get an error in a flow the ‘Catch’ code fails at the Select action with an error: BadRequest. The ‘from’ property value in the ‘select’ action inputs is of type ‘String’. The value must be an array. The From property is “result(‘Scope’)” where Scope contains my main flow. The From inputs seems to be an array containing a single record: [{“name”:”If_this_is_an_automation_task”,”inputs”:{“expressionResult”:true},”startTime”:”2022-09-06T12:12:40.1051857Z”,”endTime”:”2022-09-06T12:12:42.8708559Z”,”trackingId”:”3d7…175″,”clientTrackingId”:”085…U21″,”code”:”ActionFailed”,”status”:”Failed”,”error”:{“code”:”ActionFailed”,”message”:”An action failed. No dependent actions succeeded.”}}].
Any idea why this is not working as intended?
Is the input to the select appearing as a pill/dynamic block or as a string result(‘Scope’)?
It appears as a block (ie, as an expression not text). It looks the same as in your video and when I hover over it displays “body(‘Filter_array_for_Scope’)” which refers to the previous step. The input looks OK to me, but it obviously does not like it for some reason.
Could it be the level? Result will only return first level, you cannot return nested values from what I recall.
Thanks for the replies. My outputs from the Filter array look the same as yours in the video, so I’m not sure what’s wrong. However, I’ve decided to drop the HTML table part and without those two steps (‘Select’ and ‘Create HTML table’) it works fine and writes to my failures table which was my main objective. Many thanks again for posting this.
Hi, thanks a lot for this video. I am a big fan, you are always an inspiration for me (I’m serious! :-)).
Question: I have already implemented this process to record multiple failed&passed flows in Sharepoint, and PowerBI is very efficient to display the data. But I’m still facing an issue: Maybe I am wrong but every connector returns the result() JSON string in a different way. Indeed i.e. in the scope “Try” if the failed action is:
– a Compose: error message is in the JSON @position error/message
– a Refresh a PowerBI dataset: error message is in the JSON @position outputs/body/error/message
– an HTTP Request: error message is in the JSON @position outputs/body/fault/detail/errorcode
In your video we can see that there is no error message in the HTTP Request action.
Do you confirm this situation and do you have any idea to get systematically the error message independently to the action?
Thanks for your help!!!
Franck