Highlight

Try / Catch / Finally programming blocks are the staple of any enterprise ready solution, and you can use this pattern in Logic Apps too!

Intro

If you ever developed in any programming language then try/catch/finally being a massive help when building resilient solutions isn’t any news to you, but what might be a news to you is that you can replicate this pattern in Logic Apps.

For those unaware, the pattern is simple and it looks more or less like

  1. Try to execute all these actions
    1. Action #1
    2. Action #2
    3. Action #3
  2. Catch any error if it occurs and then run these actions
    1. Action #1
    2. Action #2
  3. Finally regardless if actions from block 1 were successful or any errors occurred in them, execute thse actions
    1. Action #1
    2. Action #2

This pattern works in both logic apps standard and consumption SKUs. The only thing you need is to use ‘Run After’ in conjunction with ‘scope’ actions.

Don’t like reading? Watch my video?

… video here…

Scope Action

Scope action allows you to encapsulate one or more actions in into a larger block of code, but what is great is that when one action within that block fails, you skip remaining actions, but act on it after scope block end.

A few things that scope action is great for

  1. Encapsuating actions into logical blocks allowing for easier management of larger logic apps
  2. Ability to capture errors from one or more actions within the scope block
  3. Ability to continute after any action failed i.e. ‘jump to end of block’ when something fails, and react to it

Run After

This setting is a condition that allows you to set how your logic app actions behave. By default it’s set to ‘Run After : Succeeded’. This means logic app will only execute next action, if previous action was successful.

All statuses for Run After are

  • Succeeded - run action previous (or selected) action(s) succeed (green - 🟢)
  • Failed - run action previous (or selected) action(s) failed (red - 🔴)
  • Skipped - run action previous (or selected) action(s) is skipped (gray - ◻️), this means previous action didn’t run at all, mostly because it wen’t into if-else condition, or previous action didn’t meet its ‘RunAfter’ condition.
  • Timed Out - run action previous (or selected) action(s) run but timed out for whatever reason, often meaning maximum execution time (gray - ◻️)

Try / Catch / Finally

If you use both RunAfter and Scope action you can easily replicate this fantastic pattern. Here is how.

  1. Add three scope blocks in below order (those are just basic logic app actions from the Control connector)
    1. One called ‘Try
    2. One called ‘Catch
    3. One called ‘Finally
  2. Set Catch scope setting for RunAfter to Failed
    • In case entire block failed, you need to handle errors
  3. Set Finally scope setting for RunAfter to all available settings (succeeded, failed, skipped, failed)
    • Succeeded - means catch block executed hence there were errors
    • Failed - means catch block executed hence there were errors, but something failed in catch statement
    • Skipped - means there were no errors
  4. Add your core logic into try scope
  5. Add your error handling logic into catch scope
  6. Add your finalization logic into finally scope

Example logic app workflow

  1. SharePoint new file trigger
  2. Try scope
    1. Log in database a new entry that the processing started
    2. Download file from SharePoint
    3. Put file into Azure Blob Storae
    4. Execute data factory pipeline
    5. Log in database a new entry that the processing succeeded
  3. Catch scope
    1. Log in database a new entry that the encountered the issue with error message from the try scope
    2. Send teams message to ops team that something has failed
    3. Create Azure DevOps or ServiceNow task to ops team
    4. Log error in an ops database
  4. Finally 1 . Raise error so logic app workflow fails with custom stats message - easier to ops to find this workflow and it shows in the logs as failed

Summary

Now that’s how you make logic apps that never fail in unintended way.

Did you found this useful? Did you know you can make this? If so, let me know, I hope you will find this useful :)

Adam Marczak

Programmer, architect, trainer, blogger, evangelist are just a few of my titles. What I really am, is a passionate technology enthusiast. I take great pleasure in learning new technologies and finding ways in which this can aid people every day. My latest passion is running an Azure 4 Everyone YouTube channel, where I show that Azure really is for everyone!

Did you enjoy the article?

Share it!

More tagged posts