Reference Exception Strategy in Mule

Reference Exception Strategy in Mule

This is to refer to a common exception strategy which you can define in a separate configuration file. You can create one or more global exception strategies to reuse in flows throughout your entire Mule application.

You can create one or more global exception strategies in your Mule application to reuse in your flows. First, create a global exception strategy, then add a reference strategy exception to the flow to apply the error-handling behavior of your new global catch exception strategy.

For this, add a Reference Exception Strategy to a flow to refer the error handling the behavior of a specific global exception strategy.

For this we need to create two exception files :

1. flow.xml
2. global.xml

flow of reference exception strategy :

flow of global.xml :

Configuration of reference exception strategy

We need to give flow name present in global.xml

URL : http://localhost:8085/api/ex/{id}
Method :POST

Input :

Output :

XML project code :


<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:metadata="" xmlns:tracking="" xmlns:dw="" xmlns:json="" xmlns:http="" xmlns="" xmlns:doc="" xmlns:spring="" xmlns:xsi="" xsi:schemaLocation=""> <http:listener-config name="HTTP_Listener_Configuration" host="" port="8085" doc:name="HTTP Listener Configuration" basePath="/api"/> <flow name="ref_exceptionFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/ex/{id}" doc:name="HTTP" /> <set-variable variableName="var1" value="#[message.inboundProperties.'http.uri.params'.id]" doc:name="Variable" /> <dw:transform-message doc:name="Transform Message"> <dw:set-payload><![CDATA[%dw 1.0 %output application/xml --- { id : }]]></dw:set-payload> </dw:transform-message> <set-payload value="#[payload]" doc:name="Set Payload" /> <exception-strategy ref="exception_demoChoice_Exception_Strategy" doc:name="Reference Exception Strategy"/> </flow> </mule>

global.xml :

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:cluster="" xmlns:http="" xmlns="" xmlns:doc=""
    <configuration defaultExceptionStrategy-ref="exception_demoChoice_Exception_Strategy" doc:name="Configuration">
        <http:config useTransportForUris="false"/>
 <choice-exception-strategy name="exception_demoChoice_Exception_Strategy">
<catch-exception-strategy doc:name="choiceexceptionhandling" when="#[exception.causeMatches('com.mulesoft.weave.*')]">
<set-property propertyName="http.status" value="400"
doc:name="Property" />
<set-payload value="input is not correct" doc:name="Set Payload" />
<catch-exception-strategy doc:name="choiceexceptionhandling1">
<set-property propertyName="http.status" value="500"
doc:name="Property" />
<set-payload value="internal server error" doc:name="Set Payload" />