Handling Global Exceptions in Mule

A catch exception strategy can be defined to customize the way Mule handles messages with errors. A catch exception strategy catches all exceptions thrown within its flow and processes them, thereby overriding Mule’s implicit default exception strategy.

Mule’s catch exception strategy behavior is similar to a Java catch block, except that a new exception cannot be thrown or another exception cannot be caught within a catch exception strategy.
In my previous tutorial for local catch-exception-strategy, exception strategies can be configured on a per-flow basis. This is done by defining an exception strategy at the end of each flow definition.

For global catch-exception-strategy you need to configure different exception strategies for flows that could reuse exception strategies. To configure a global exception strategy two steps are required. You need first to configure a global exception strategy and then put a reference in the desired flows.

Let’s start by configuring an exception strategy inside a flow. Let’s use a catch-exception-strategy that will catch and process all exceptions thrown in the flow

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:batch="http://www.mulesoft.org/schema/mule/batch"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.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/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd">
	<spring:beans>
		<spring:bean name="priceCalculator"
			class="com.roytuts.mule.component.PriceCalculator" />
	</spring:beans>
	<catch-exception-strategy name="globalCatchStrategy"
		doc:name="Catch Exception Strategy">
		<set-payload value="Error: #[exception.summaryMessage]"
			doc:name="Set Payload" />
	</catch-exception-strategy>
	<flow name="global-catch-exception-strategy-flow" doc:name="global-catch-exception-strategy-flow">
		<http:inbound-endpoint exchange-pattern="request-response"
			host="localhost" port="8083" doc:name="HTTP" />
		<invoke object-ref="priceCalculator" method="calculatePriceForItems"
			doc:name="Invoke" methodArgumentTypes="java.lang.Integer,java.lang.Double"
			methodArguments="#[message.inboundProperties.'http.query.params'.items],#[message.inboundProperties.'http.query.params'.cost]" />
		<exception-strategy ref="globalCatchStrategy" />
	</flow>
</mule>

The graphical representation is shown below


mule catch exception strategy

The PriceCalculator java class source code

package com.roytuts.mule.component;
public class PriceCalculator {
    final double taxRate = 0.5;
    public String calculatePriceForItems(Integer noOfItems, Double eachItemCost) {
        return "Total Cost : Rs. " + noOfItems * eachItemCost * taxRate + "/-";
    }
}

When you run the mule application, you will see below outputs on different inputs


mule catch exception strategy


mule catch exception strategy

Console Output

ERROR 2016-11-05 21:15:24,562 [[mule-3].connector.http.mule.default.receiver.04] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message               : For input string: "3s" (java.lang.NumberFormatException). Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. For input string: "3s" (java.lang.NumberFormatException)
  sun.misc.FloatingDecimal:-1 (null)
2. For input string: "3s" (java.lang.NumberFormatException). Message payload is of type: String (org.mule.api.MessagingException)
  org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.NumberFormatException: For input string: "3s"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *