Welcome to the fourth part of this series of five blog posts on How to throw custom exceptions inside Logic Apps.
In this series of blog posts, we talk about the following:
- Part I – Using default capabilities
- Part II – Using default capabilities – Extract failure information
- Part III – Using default capabilities – Avoiding too many condition actions
- Part IV (this article) – Using a child Logic App to throw a custom exception
- Part V – Using an API Management Throw Exception API
Thinking a little bit out-of-the-box, we can actually use a Logic App that has a throw exception mechanism. So, in this approach, what we pretend to do here is to create a generic Logic App Consumption that can dynamically raise the error back to the primary process. This way, we can reuse it in all our processes without having to re-implement the same actions over and over and minimize the use of shapes/actions inside our workflow.
Approach 4: Using a child Logic App to throw a custom exception
The simple solution for this approach is to:
- Create a simple Logic App using the HTTP Request-Response template. Let’s call it LA-EAI-Common-ThrowException
- Leave the Request When a HTTP request is received trigger as is.
- In the Response action, set the following configuration:
- On the Status Code property, set to 500.
- On the Body property, set as triggerBody().
That means that any plain text message or JSON message we pass to this common Logic App will be returned as an HTTP Status failure back to the main Logic App that can catch this exception.
However, we want this to be consistent with the generic error handling described in one of our previous whitepapers: Logic Apps Consumption – How to get the Logic App error detail message guide. This way, we don’t need to implement different capabilities in our catch scopes, and they will work consistently, no matter the approach.
Therefore, let’s improve our common Logic App to include more details. To do that, we need to:
- Access and expand the Request When a HTTP request is received trigger, and then select Use sample payload to generate schema.
- On the Enter or paste a sample JSON payload page, copy and paste the following JSON and click Done.
{ "ErrorMessage": "Invalid Age! You need to be older than 18." }
- Now we need to access the Response action and make the following changes:
- On the Body property, add the following expression:
- triggerBody()?[‘ErrorMessage’]
- On the Body property, add the following expression:
Now save your Logic App.
Next, we need to get back to our main Logic App. In this fourth approach, we are going to do some fine-tuning of the previous approach and redesign the way we throw exceptions. To explain better, let’s use the previous version that contains multiple Conditions.
Now we need to:
- Delete the Initialize varSupportForceError variable.
- Expand the Try Scope, expand the Check if name is John condition, and on the False branch:
- Delete all actions.
- Do the same on the Check if Age is Less than 18 condition.
- Now inside the Try Scope -> Check if name is John -> False branch, and click Add an action.
- In the search box, enter Logic App, and from the result panel, select the Azure Logic Apps connector, and from the list, select the common Logic App we create earlier: LA-EAI-Common-ThrowException.
- From the action, select the only option available: manual.
- On the LA-EAI-Common-ThrowException action, add the following configurations:
- On the ErrorMessage property, set the error message, in this case:
- @{triggerBody()?[‘name’]} is an Invalid Name!
- On the ErrorMessage property, set the error message, in this case:
- In the Invalid Name ThrowException action, click on … (3 dots) and then select the Settings option.
- On the Settings for ‘Invalid Name ThrowException’ panel, set the Retry Policy property to None and click Done.
- Now inside the Try Scope Check if Age is Less than 18 -> False branch, click Add an action.
- In the search box, enter Logic App, and from the result panel, select the Azure Logic Apps connector, and from the list, select the common Logic App we create earlier: LA-EAI-Common-ThrowException.
- From the action, select the only option available: manual.
- On the LA-EAI-Common-ThrowException action, add the following configurations:
-
- On the ErrorMessage property, set the error message, in this case:
- Invalid Age! You need to be older than 18.
- On the ErrorMessage property, set the error message, in this case:
-
- In the Invalid Age ThrowException action, click on … (3 dots) and then select the Settings option.
- On the Settings for ‘Invalid Age ThrowException’ panel, set the Retry Policy property to None and click Done.
- Now let’s expand the Catch Scope to make the final modifications.
- On the Catch Scope, expand the Check if varResultMessage contains a custom error message condition and drag the two actions inside the True Branch to outside the condition.
- Then, delete the Condition action.
Save the Logic App.
If we try our solution again, we will get our custom error description.
{ "Result": "Invalid Age! You need to be older than 18." }
A few of the most important advantages of this solution are:
- To have a good error description of our custom exceptions and an easy way to throw them.
- It is a solution that is simple to implement.
- We don’t need to use too many support actions or use many actions anytime we need to raise an exception.
- The way it is implemented is compatible with any runtime failure, which means we don’t need to implement different error-handling conditions inside our catch blocks.
However, there are some drawbacks also:
- If we have too many executions in parallel to this common Logic App, you may face some performance constraints.
- Additional payment for each exception we need to throw.
- You can have a single common Logic App Consumption to throw errors, but if you are using Logic App Standard, you need to use the HTTP connector to call this child Logic App.
- Or you need to create a replica of this process in Standard.
Nevertheless, this is, in our opinion, one of the best approaches you can implement.
I hope you enjoy and stay tuned for part V.