Error Handling in Mule 4 | Mule 4 Exception Handling Types

Error Handling in mule 4


Error handling in mule 4 is done by:
1. Error Handler
2. On error continue
3. On error propagate
4. Raise error

The first Scenario is handling of error by: On error continue


On error continue settings:


On error continue handles the error and control moves to next component and gives 200 success response code. In this component, control does not move to the message source after the error occurs.

The second scenario is error handling by: on error propagate

On error propagate settings:


On error propagate logs the error if it occurs and gives error code and control moves to messaging source.

The third scenario is the error handler.
We configure the error handler to the global error handler. The global error handler is common to all flows in an application.
To configure it, go to:
global elements -> create -> global configurations-> configurations


The flow of global error handler:


We can configure on error continue or on error propagate in it.

Fourth scenario is: Raise error.
It throws an error with the specific type and description.

Flow:
 
Raise error settings:


Handling of error takes place at three levels:
1. Processor level
2. Flow level
3. Application level


XML project code:

<?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="90060ac9-ab94-4e7d-8779-5edb29635514" >
          <http:listener-connection host="0.0.0.0" port="8085" />
     </http:listener-config>
     <configuration doc:name="Configuration" doc:id="57a21529-8e04-4148-8d98-1e840341be1d" defaultErrorHandler-ref="test_error_handlingError_Handler" />
     <flow name="test_error_handlingFlow" doc:id="85132160-a53f-4b94-84e8-58f28d370212" >
          <http:listener doc:name="Listener" doc:id="6c72fd63-e2e2-4b5c-96cf-96e103300a55" config-ref="HTTP_Listener_config" path="/continue"/>
          <try doc:name="Try" doc:id="b934ba3a-2a17-4621-a898-ec6d3496da39" >
              <ee:transform doc:name="Transform Message" doc:id="fc27848c-2ba3-4382-b6ab-01edff9183cb" >
                   <ee:message >
                        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
     id: payload.id as String
}]]></ee:set-payload>
                   </ee:message>
              </ee:transform>
              <error-handler >
                   <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="8930dbae-a100-4eae-bd26-9f9074a9eea0" type="ANY">
                        <logger level="INFO" doc:name="Logger" doc:id="be5b001b-9852-4756-aeed-ee0d66080ca4" message="in processor level on error continue"/>
                   </on-error-continue>
              </error-handler>
          </try>
          <logger level="INFO" doc:name="Logger" doc:id="1d75ec01-86b4-47de-b996-53d61f834c80" message="last logger"/>
          <error-handler >
              <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="4d97d116-0700-48de-b233-d6247255632d" type="ANY">
                   <logger level="INFO" doc:name="Logger" doc:id="79f4203a-339c-4a43-8f9c-190b2145bd08" message="in flow level on error continue"/>
              </on-error-continue>
          </error-handler>
     </flow>
     <flow name="test_error_handlingFlow1" doc:id="77f2c5fa-7009-4bb1-8279-c7ba81a41951" >
          <http:listener doc:name="Listener" doc:id="d0493321-6491-4a38-8429-3cf0878e6240" config-ref="HTTP_Listener_config" path="/propagate"/>
          <try doc:name="Try" doc:id="aa6b898b-37e9-4239-b3c5-ce8c4b22abcc" >
              <ee:transform doc:name="Transform Message" doc:id="bb9017df-254b-4480-8ec5-08912cdff3c8" >
                   <ee:message >
                        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
     id:payload.id as String
}]]></ee:set-payload>
                   </ee:message>
              </ee:transform>
              <error-handler >
                   <on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="37f12222-c1ec-4768-81f5-522ad08582ac" type="ANY">
                        <logger level="INFO" doc:name="Logger" doc:id="b1eb3fef-cbc3-48bb-88bb-cf6621a1aa3b" message="in processor level on error propagate"/>
                   </on-error-propagate>
              </error-handler>
          </try>
          <logger level="INFO" doc:name="Logger" doc:id="27252a68-3cb7-497d-9f1c-3bd8af83d537" message="last logger"/>
          <error-handler >
              <on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="7c62b539-a945-4146-9b7b-371c016c078f" type="ANY">
                   <logger level="INFO" doc:name="Logger" doc:id="61efff1b-48e1-46b5-ac1a-5cc03acaea87" message="in flow level on error propagate"/>
              </on-error-propagate>
          </error-handler>
     </flow>
     <error-handler name="test_error_handlingError_Handler" doc:id="a0a6cae1-1495-4693-9367-9bb1da3756cc" >
          <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="0f050e12-6a6e-46f6-ae8f-244c408fde08" type="ANY">
              <set-payload value="#[error.description]" doc:name="Set Payload" doc:id="0f414867-0aa8-4a32-b66e-27020db98734" />
          </on-error-continue>
     </error-handler>
     <flow name="test_error_handlingFlow3" doc:id="a0a35bca-f1b0-4c11-98d0-ed1dd01b551e" >
          <http:listener doc:name="Listener" doc:id="26f8be5c-5865-4246-90d0-e34f6215da5a" config-ref="HTTP_Listener_config" path="/raise"/>
          <try doc:name="Try" doc:id="3c105615-bc89-48e3-8bc4-50b66dc5906a" >
              <ee:transform doc:name="Transform Message" doc:id="68495cb9-5151-4b20-9542-8969fb9cdc8c" >
                   <ee:message >
                        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
     id:payload.id as String
}]]></ee:set-payload>
                   </ee:message>
              </ee:transform>
          </try>
          <raise-error doc:name="Raise error" doc:id="6ec851ea-4b47-4264-96c4-37ff0d41d95f" type="EXPRESSIONS: INCORRECT" description="Dataweave expression is incorrect" />
          <logger level="INFO" doc:name="Logger" doc:id="64e1e27b-89e3-4088-81cd-fef2912c2549" message="last logger"/>
     </flow>
</mule>