Code Snippets

Solving 3 Types of Mule Errors

Here are 3 code snippets for solving different errors that can occur in MuleSoft. Refer to this blog post for more information.

1. Define an Error Handler Within a Flow

You can define error handling that is encapsulated within a single flow and is not referenced by other flows in your application. This would look something like this:
<flow name="sampleFlow" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
<catch-exception-strategy doc:name="Catch Exception Strategy"> ... do something
</catch-exception-strategy> </flow> 

To Note: Normal flows can implement or refer to a global error handler, private flows inherit the parent error handler, but can define a different error handler to use, and sub-flows only can inherit the error handler of the parent/calling flow.

2. Define a Global Error Handler

You can define a global error handler that can be referenced from any flow (except sub-flows, which inherit the calling flow error handler). This would look something like this:
<catch-exception-strategy name="testGlobalErrorHandler" doc:name="Catch Exception Strategy"> ... do something
</catch-exception-strategy>
<flow name="sampleFlow1" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
<exception-strategy ref="testGlobalErrorHandler" doc:name="Reference Exception Strategy" />
</flow>
<flow name="sampleFlow2" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
<exception-strategy ref="testGlobalErrorHandler" doc:name="Reference Exception Strategy" />
</flow> 

To Note: You can define as many global exception strategies as you want to use within your application flows.

3. Define an Custom Default Global Error Handler

You can define a global exception strategy that can be designated as a custom default global error handler. This would look something like this:
<catch-exception-strategy name="testGlobalErrorHandler" doc:name="Catch Exception Strategy"> ... do something
</catch-exception-strategy>
<configuration defaultExceptionStrategy-ref="testGlobalErrorHandler" doc:name="Configuration" doc:description="Use as implicit default exception strategy."/>
<flow name="sampleFlow1" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
</flow>
<flow name="sampleFlow2" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
</flow>
<flow name="sampleFlow3" doc:name="sampleFlow">
<http:inbound-endpoint address="${main.inbound.http}" doc:name="HTTP" />
<catch-exception-strategy doc:name="Catch Exception Strategy"> ... do something
</catch-exception-strategy>
</flow> 

To Note: In the above example, sampleFlow1 and sampleFlow2 would use the global default error handler, and sampleFlow3 would use the embedded error handler. 

 

 

Resource Tags: