Error Handling Using Try Scope in Mule 4

DZone 's Guide to

Error Handling Using Try Scope in Mule 4

A developer with expertise in Mule discusses a feature recently released with Mule 4, try scope, and what it allows developers to do.

· Integration Zone ·
Free Resource

With the introduction of Mule 4, there are many new features available to use. One among them is "Try Scope." If you have a Java background, you will be familiar with this term. In Java, a try block is used to enclose the code which has the possibility of being an exception, so that it can be handled without breaking the whole code.

Similarly, this feature is now enabled in Mule 4. Instead of creating a new flow to create specific error handling protocols for each component, we can put our component in the try block. A try scope wraps one or more event processors, then catches and handles any exceptions that might be thrown by any of these enclosed event processors. The behavior is as if you extracted those enclosed event components into a separate flow with its own error handling strategy, but inline, without having to actually define a new flow.

Like all error handling, the try scope can also distinguish between various error type conditions and apply different behaviors to each error type condition. Just like any error that is raised inside a flow, if an error is raised by a component inside a try scope, then the try scope’s error handler is executed, and the error is routed to the matching handler. At this point, the error is available for inspection, so the handlers can execute and act accordingly:

  • An On Error Continue will execute and use the result of the execution as the result of its owner as if the owner actually completed the execution successfully. Any transactions at this point are also committed.
  • An On Error Propagate will roll back any transactions, execute, and use that result to rethrow the existing error, meaning that its owner will be considered to be failing.

If the try scope has several components, then once a component raises an exception, subsequent components in the try scope are skipped, regardless of the type of error handler that catches the exception. In the case of On Error Propagate, the error is propagated to the flow’s error handling, as if the try scope did not exist. In the case of On Error Continue, processing continues outside the try scope at the next downstream flow component, as if the try scope never threw an exception.

Handling Transactions

You can configure a try scope so that it treats its child operations as indivisible transactions. A transaction is a series of actions that should never be partially executed. Every operation within the scope of a transaction is executed in the same thread, and errors should lead to either a rollback or a commit.

Image title

The try scope treats child operations as a transaction when the transactional action (transactionalAction) is set to ALWAYS_BEGIN or BEGIN_OR_JOIN. It can be configured in the following ways:

  • Ignore (INDIFFERENT): Default. Actions are not treated as a transaction. Errors cause no rollbacks or commits.
  • Always Begin (ALWAYS_BEGIN): A new transaction is started every time the scope is executed.
  • Begin or Join (BEGIN_OR_JOIN): Only relevant in cases where execution order may vary (for example, due to asynchronous actions occurring outside the flow). If the current processing of the flow has already started a transaction, join it. Otherwise, begin a new one.



Image title

In the example above, any connection errors are propagated because of the On Error Propagate (on-error-propagate) setting. Propagation of this error causes the try process to fail and the flow’s error handler to execute. Other errors are handled through On Error Continue (on-error-continue), so the try is treated as successful when they occur, meaning that the next processor continues to execute.

Here's some example XML of the processes described in this article:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="5c46f87f-5c93-4073-8a22-c0b9b6e91153" >
<http:listener-connection port="8081" host=""/>
<flow name="try1" doc:id="c675a407-c317-41bd-9bf7-fc8683f29e7d" >
<http:listener doc:name="Listener" doc:id="d812b83a-90ae-429d-a11f-b58bab1ea948" config-ref="HTTP_Listener_config" path="/Trytest" allowedMethods="GET">
<http:response >
<http:headers ><![CDATA[#[output applicaton/java
user : "01-12-19923"
<set-variable value="#[message.attributes.'http.query.params'.user]" doc:name="Set Variable" doc:id="9763df48-94ba-40d9-971d-8d858d391602" variableName="user" />
<try doc:name="Try" doc:id="10ea4948-cd0f-40ec-92f8-b0dc6025d5c9" >
<choice doc:name="Choice" doc:id="8f7b07e4-8cf2-4746-bb2c-2664453033d3" >
<when expression="#[flowVars.user == 'Apple']" >
<set-payload value="#['Hello'+flowVars.user+', Welcome to Mule']" doc:name="Set Payload" doc:id="4c9425dd-240c-486b-b2e7-800f6b0352e9" />
<when expression="#[flowVars.user == 'Banana']" >
<set-payload value="#['Hello'+flowVars.user+', Welcome to Mule']" doc:name="Set Payload" doc:id="37cd62f5-0351-42ea-b81e-7ed8e0e45a18" />
<otherwise >
<set-payload value="#['Not a valid Name']" doc:name="Set Payload" doc:id="4310b1e1-3bb5-4c00-9b92-b9c2f90297eb" />
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="eef2712c-2a21-4738-9fb0-935550c305d9" type="Expression">
<ee:transform doc:name="Transform Message" doc:id="3f6aae67-45e3-4398-ab65-39915e168ab5">
<ee:set-payload><![CDATA[%dw 2.0
output application/json
var abc = "Hello"
var regexTry = "(?:ST|ND|RD|TH)"

{stick : "ND" contains "(?:ST|ND|RD|TH)"
<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="5aba1fed-3eae-4736-b1e9-7d0ef7beab10" type="Connection">
<logger level="INFO" doc:name="Logger" doc:id="f2eaa21a-3a36-44d0-b1fe-40fe2cd8a6c2" />
<logger level="INFO" doc:name="Logger" doc:id="4b774fdf-dc2b-4f74-820f-e98f3647e9fa" message="#[payload]"/>
integration, mule 4, transactions, try block, try scope

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}