Handling Local 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.
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.

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>

    <flow name="local-catch-exception-strategy-flow" doc:name="local-catch-exception-strategy-flow">

        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8082" 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]" />

        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="Error: #[exception.summaryMessage]" />
        </catch-exception-strategy>

    </flow>

</mule>

Here you can find one new element, the catch-exception-strategy. This element instructs the flow to respond with a String that summarizes the exception message, for any kind of error.

The graphical representation is shown below


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


catch-exception-strategy


catch-exception-strategy


catch-exception-strategy

Console Output

ERROR 2016-11-04 08:42:11,884 [[mule-3].connector.http.mule.default.receiver.04] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : For input string: "s" (java.lang.NumberFormatException). Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. For input string: "s" (java.lang.NumberFormatException)
  sun.misc.FloatingDecimal:-1 (null)
2. For input string: "s" (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: "s"
	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)
********************************************************************************

ERROR 2016-11-04 08:42:45,398 [[mule-3].connector.http.mule.default.receiver.04] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : null (java.lang.NullPointerException). Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. null (java.lang.NullPointerException)
  org.mule.processor.InvokerMessageProcessor:276 (null)
2. null (java.lang.NullPointerException). 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.NullPointerException
	at org.mule.processor.InvokerMessageProcessor.transformArgument(InvokerMessageProcessor.java:276)
	at org.mule.processor.InvokerMessageProcessor.evaluateArguments(InvokerMessageProcessor.java:196)
	at org.mule.processor.InvokerMessageProcessor.process(InvokerMessageProcessor.java:160)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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