Search code examples
exceptiontry-catchazure-logic-apps

How to throw an exception in Azure Logic Apps?


Is it possible to throw an exception in a Logic App?

Specifically, I'm trying to throw an exception inside of a scope. Then in the run after of the scope I'd check if it failed and inspect it for errors. I tried using a terminate inside of a scope, but that terminates the entire logic app run.


Solution

  • Quick Answer (TL;DR)

    • Problem: MSFT Azure logic app throwing exceptions
    • Workaround: Use logic app Scope element to simulate throwing exceptions
      • Create a scope element to use as your "try-catch" block
      • Force the scope element to fail with an invalid command to simulate an exception
      • Allow the scope element to fail naturally and that will count as an exception too

    Detailed Answer

    Context

    • MSFT Azure logic app workflows
    • live version as of 2020-06-14

    Problem

    • Scenario: Developer wishes to throw an exception or use try-catch semantics in a logic app

    Solution

    • As of this writing, the live version of the product does not support this feature. There is a workaround.

    Workaround

    • Use logic app scope element
    • add a conditional statement inside the scope element
    • If the conditional statement meets the failure condition, force an exception with a deliberately invalid command
      • for example create a variable assignment with the value int('__ERROR__')
    • If the conditional statement does not meet the failure condition, do nothing
    • The rest of the logic app consists of two paths
      • The first path runs on the success of the scope element
      • The second path runs on the failure of the scope element (failed, skipped, timed out)

    Example

    • Create a scope element as a "try-catch" block

    using scope element to simulate throwing an exception

    • Create a variable compose element with an invalid command invalid variable compose
        
        int('__ERROR__')  ## this will cause the enclosing scope to fail
                          ## the string __ERROR__ cannot be cast to integer
    
    • Respond to exit status of the Scope element enclosing your exception Respond to the exit status of the enclosing Scope

    See also