2

Does anybody know how to handle Unchecked / Runtime Exceptions in MULE..??

I mean, in my java code, for a certain reason , i am "throwing an Exception" and i want Mule to detect it and route it to proper flow , where i can Log or Print that Exception.

So , What exactly should i place in my "flow" in Mule Config File to achieve that.

My Java code :

public Object xyz (Map payload) throws Exception {
    if (payload.isEmpty()) {
        throw new Exception ("New Exception") ;
    }
}

My Mule Config file :

<flow name="APIAuthenticate">

    <http:inbound-endpoint address="http://localhost:1212/jcore/authorize" transformer-refs="HttpParams" responseTransformer-refs="JavaObjectToJson" contentType="application/json" encoding="UTF-8">
        <not-filter>
            <wildcard-filter pattern="/favicon.ico"/>
        </not-filter>
    </http:inbound-endpoint>

    <component class="main.java.com.abc.XYZ"/>

</flow>

Any help will be deeply appreciated..!!

Ramandeep Singh
  • 5,063
  • 3
  • 28
  • 34

2 Answers2

2

Configuring a default-exception-strategy in your flow should allow you to catch the exceptions (even runtime ones) and deal with them.

Read the error handling reference guide for more info.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • i know "default-exception-strategy" handles the error. But i always throw a 500 Server error at the client side (the one who hit that URL). I want to Show some neat Exception to the Client. in form of String or some HashMap. Now tell me , what should i do..?? – Ramandeep Singh Nov 09 '11 at 04:24
  • You're saying your default-exception-strategy handles the thrown exception but still a 500 is returned to the client? – David Dossot Nov 09 '11 at 16:36
  • Yes..I think what "default-exception-strategy" does is, it catches an Error and route it some other flow, where we can "log that error" or "output it to some file or console". But to the client , it always show "Server Error"..!! – Ramandeep Singh Nov 17 '11 at 05:46
  • OK, but if what if your exception handler returns another message, like an error one? – David Dossot Nov 17 '11 at 06:12
  • the flow will be broken , no log would be generated , but client would be shown the usual "Server Error" message. – Ramandeep Singh Nov 17 '11 at 06:19
  • then in the main flow add a choice that detects the presence of an exception payload and based on that builds an error response (similar to what I demonstrate in your other question: http://stackoverflow.com/questions/8154508/how-to-edit-default-mule-error-message ) – David Dossot Nov 17 '11 at 06:31
  • Thats what i said in my answer to this question, that "Throwing an exception" is different from "returning an Exception Object". Payload filtering is only possible when we return an Exception object, rather than throwing an Error Object..I want to catch "Exception that is THROWN not returned"..!! – Ramandeep Singh Nov 17 '11 at 06:46
  • I understand that but that is not the way it should be. When you return an exception, you store it in the main message payload while when an exception is handled by Mule, it leaves the main payload unchanged and stores it in the exception payload of the message. See: http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleMessage.html#getExceptionPayload() This is why returning exceptions should never be needed. – David Dossot Nov 17 '11 at 15:59
  • Ok..i'll see to it..thanx..!! – Ramandeep Singh Nov 18 '11 at 06:24
2

Ok..i did some hit and trial and i figured out that

  • When the Exception is thrown, an Exception Strategy is required like default-exception-strategy OR custom-exception-strategy is required, that would route the flow to some Class that would handle it and do required Actions.

  • But When we Return an Exception (like below), then we can use the exception-payload-filter or choice attribute of Mule to handle it.

    public Object xyz (Map payload) throws Exception {
        if (payload.isEmpty()) {
            return new Exception ("New Exception") ;
        }
    }
    

Please Correct me if i am wrong..??

Also if there are other answers to this question, please be kind to put them..

Ramandeep Singh
  • 5,063
  • 3
  • 28
  • 34