Improving Workflow Error Management in Power Automate with TRY-CATCH

Improving Workflow Error Management in Power Automate with TRY-CATCH

In this article, I’ll show you how to set up a TRY-CATCH pattern in Power Automate workflows to improve error detection and resolution in production environments. This setup ensures that both developers and admins are notified immediately when an error occurs, with all the context they need to act fast.

Why Use a TRY-CATCH Pattern?

Errors in production workflows can often go unnoticed, leaving admins scrambling to fix issues without proper context. Developers, on the other hand, may not even know an issue exists until much later. By implementing a TRY-CATCH structure, you can:

  • Detect errors as soon as they happen.
  • Notify admins and developers with critical details about the workflow and the error.
  • Save time and reduce the impact of production issues.

Step-by-Step Setup

1️⃣ Add a TRY Scope

In your Power Automate workflow:

  • Create a Scope action named TRY.
  • Add all your workflow’s main actions inside the TRY scope, except for variable declarations.

The TRY scope will execute your workflow logic and attempt to run all actions.

2️⃣ Add a CATCH Scope

  • Add a second Scope action named CATCH.
  • Configure the CATCH scope to run only if the TRY scope fails, is delayed, or times out.

3️⃣ Capture Workflow Properties

Use the workflow() function to retrieve metadata about the current workflow. Add a Compose action and configure it like this:

  • Inputs:
				
					workflow()
				
			

4️⃣ Parse Workflow Properties

To extract specific details like the flow name, environment ID, and run ID, add a Parse JSON action:

  • Content: Use the Outputs of the workflow Compose action.
  • Schema: Use the following schema:
				
					{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "tags": {
            "type": "object",
            "properties": {
                "flowDisplayName": {
                    "type": "string"
                },
                "environmentName": {
                    "type": "string"
                }
            }
        },
        "run": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        }
    }
}

				
			
This action extracts:
  • Environment ID: tags.environmentName
  • Flow ID: name
  • Run ID: run.name

5️⃣ Construct the Workflow Run Link

Admins need an easy way to navigate to the failed workflow run. Construct the link using the parsed values:

				
					https://make.powerautomate.com/environments/[--EnvironmentID--]/flows/[--FlowID--]/runs/[--RunID--]

				
			

Replace the placeholders with the dynamic outputs from the Parse JSON step:

  • [–EnvironmentID–]: tags.environmentName
  • [–FlowID–]: name
  • [–RunID–]: run.name

6️⃣ Send an Email to Admins

Add a Send an Email (V2) action to notify the admins:

  • To: Use an environment variable containing admin email addresses.
  • Subject: Include the workflow name and error type (e.g., “GET items from SharePoint – Error”).
  • Body: Workflow History link : https://make.powerautomate.com/environments/[–EnvironmentID–]/flows/[–FlowID–]/runs/[–RunID–]

7️⃣ Terminate the Workflow

Add a Terminate action as the final step in the CATCH scope:

  • Status: Failed
  • Error Code/Message: Include any relevant error details for tracking.

Benefits of the TRY-CATCH Pattern

  • Immediate Notification: Admins are notified with all the context they need.
  • Clear Error Logging: Error details and the failed run link are logged for easy access.
  • Improved Collaboration: Both developers and admins can work together to resolve issues faster.

Conclusion

Implementing a TRY-CATCH pattern in Power Automate workflows is a best practice for managing production issues. With error notifications, detailed context, and direct access to failed runs, you’ll save time and improve reliability in your workflows.

Ready to make your workflows production-ready? Let me know how you’re implementing error management in your flows! 🚀

Leave A Comment