DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • Build a Flow Collectibles Portal Using Cadence (Part 2)
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Using Java Stream Gatherers To Improve Stateful Operations
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • A Guide to Auto-Tagging and Lineage Tracking With OpenMetadata

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.

By 
Sahiba Goyal user avatar
Sahiba Goyal
·
Apr. 04, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
21.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Example:

Flow:

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="http://www.mulesoft.org/schema/mule/core"
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="0.0.0.0"/>
</http:listener-config>
<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"
}]]]></http:headers>
</http:response>
</http:listener>
<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>
<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" />
</when>
<otherwise >
<set-payload value="#['Not a valid Name']" doc:name="Set Payload" doc:id="4310b1e1-3bb5-4c00-9b92-b9c2f90297eb" />
</otherwise>
</choice>
<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:message>
<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)"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</on-error-continue>
<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" />
</on-error-propagate>
</error-handler>
</try>
<logger level="INFO" doc:name="Logger" doc:id="4b774fdf-dc2b-4f74-820f-e98f3647e9fa" message="#[payload]"/>
</flow>
</mule>
Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • Build a Flow Collectibles Portal Using Cadence (Part 2)
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!