Joe Eng
Joe Eng

Reputation: 1214

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.

Upvotes: 8

Views: 10118

Answers (4)

dreftymac
dreftymac

Reputation: 32390

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

Upvotes: 6

Kamran
Kamran

Reputation: 1380

As an updated solution we can use Terminate control, Terminate control has 3 status: Failed, Canceled, and Succeeded.

enter image description here

Upvotes: 10

It seems like there still is no option for this inside Logic App or its little brother Power Automate / Microsoft Flow.

The way I have come up with and have used in some flows, is I simply add an action for something I know for a fact will fail.

The simplest (and probably the cheapest as the built-in actions cost less in Logic Apps, even if we are talking fractions of a dollar here either way) is probably to initialize a variable, e.g. called ThrowException with type of integer. Then a "Set variable" action is added wherever I want my flow to fail, where I set the value (remember it is of type integer) to any string expression. I simply use the expression string('Exception').

Simple example screenshot

Since the value is set via an expression this is still a valid template, but will fail upon runtime when the value is actually being set. After this, simply use parallel branches, with appropriate Run After settings, as usual.

Upvotes: 2

DTRT
DTRT

Reputation: 11040

No, there is no Action or Connector directly analogous to something like a throw in C#.

The closest you can get right now would be to do something like use another LogicApp instead of a scope from which you can return a specific status code.

Upvotes: 4

Related Questions