How to throw exception manually in Mule ESB without using java code

star picture star · Oct 14, 2013 · Viewed 8.3k times · Source

I'm new to mule, please help me on this logic. Actually in my flow i have choice message processor, Expression which is not satisfy it will go to default in choice. After that i have flow reference. I don't want message goes to default(choice) has to be populate to flow reference.Instead it has to go to choice exception strategy which i configured in that flow. So i need to throw some exception in default. Actually i have a very big flow, i made it simple to understand. Please find by xml config.

        <flow name="Choice" doc:name="Choice">
    <file:inbound-endpoint path="C:\Users\Star\Desktop\Mule\FilePath1" responseTimeout="10000" doc:name="File"/>
    <byte-array-to-string-transformer doc:name="Byte Array to String"/>
    <choice doc:name="Choice">
        <when expression="#[xpath('fn:local-name(/root/*[2])')=='book']">
            <file:outbound-endpoint path="C:\Users\Star\Desktop\Mule\FilePath2" responseTimeout="10000" doc:name="File"/>
        </when>
        <otherwise>
            <file:outbound-endpoint path="C:\Users\Star\Desktop\Mule\FilePath3" responseTimeout="10000" doc:name="File"/>
        </otherwise>
    </choice>
    <flow-ref name="SampleService" doc:name="Flow Reference"/>
    <catch-exception-strategy doc:name="Catch Exception Strategy">
        <logger message="*****#[exception]****" level="INFO" doc:name="Logger"/>
    </catch-exception-strategy>
</flow>
<flow name="SampleService" doc:name="SampleService">
    <file:inbound-endpoint path="C:\Users\Star\Desktop\Mule\FilePath4" responseTimeout="10000" doc:name="File"/>
</flow>

Kindly suggest me any standard way to throw exception in Mule.I also tried keeping

            <test:component  throwException="true"/> 

but it is throwing exception like - "The prefix "test" for element "test:component" is not bound".

Answer

David Dossot picture David Dossot · Oct 14, 2013

To use the test:component you need to declare the test namespace, as you've done for all the other namespaces Mule uses. This said, not sure you want to use test elements in your production code.

One alternative is to use the null-component that always throws an exception when it receives a message, but this doesn't give you control of the thrown exception.

The other alternative, is to throw any exception you want from a Groovy component.

Bear in mind that exceptions are wrapped by Mule infrastructure: you'll have to use the ExceptionUtils to dig in the stack and find the root cause.