13 Replies Latest reply on Jul 25, 2011 12:05 PM by joe_boy12

    Throwing Custom exception .

    madhucm

      Hi,

      In my ESB project i am throwing ActionProcessingException in one of my action pipeline.

      And i am handling this exception in AbstractActionPipelineProcessor.processException().

      Currently i am able to see Exception message as SOAPFault as response.

      But my requirement is to construct custom xml and use same custom xml for displaying error message.

      Is it possible to construct custom xml instead of showing "Exception" as SOAPFault.

       

      Please let me know is there any strandard way to do this.

       

      Thanks,

      Madhu CM

        • 1. Throwing Custom exception .
          tcunning

          Can you post your jboss-esb.xml?      I'm a little confused by what you're asking here and want to see your action chain.

          • 2. Re: Throwing Custom exception .
            madhucm

            Tom Cunningham wrote:

             

            Can you post your jboss-esb.xml?      I'm a little confused by what you're asking here and want to see your action chain.

             

            Tom,

            I have attached ESB project. Please see my comments in processException() method.

             

            I want response as :

             

            <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

               <env:Header/>

               <env:Body>

                <ProducerInScopeResponse>

                          <Exception>

                             Gone something bad!

                           </Exception>

              </ProducerInScopeResponse>

            </env:Body>

            </env:Envelope>

             

             

            instead of :

             

            <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

               <env:Header/>

               <env:Body>

                  <env:Fault>

                     <faultcode>env:Server</faultcode>

                     <faultstring>Gone something bad!</faultstring>

                  </env:Fault>

               </env:Body>

            </env:Envelope>

            • 3. Throwing Custom exception .
              h.wolffenbuttel

              Hi Madhu,

               

              What your getting is a standard SOAPFault, which can be replaced by your message by replacing the default message body after catching the fault.

               

              Regards,

               

              Hans

              • 4. Re: Throwing Custom exception .
                madhucm

                Hi hans,

                I tried the way you explained . It is not working.

                If you have any example on this i would be pleased .

                 

                Thanks,

                Madhu CM

                • 5. Re: Throwing Custom exception .
                  h.wolffenbuttel

                  Can you post the setup you have used? You might also want to look into you ESB-message properties, especially the fault. If it still is filled you might want to try to empty it. And see how that works. Another possibility is to throw a new ActionProcessingException(String message) with your message as a parameter (after catching it ofcourse).

                   

                  regards,

                   

                  Hans

                  • 6. Re: Throwing Custom exception .
                    madhucm

                    Hi Hans,

                    Please find the attached ESB project.I have used processException() method. But still i am not able to

                    • 7. Re: Throwing Custom exception .
                      h.wolffenbuttel

                      did you try something like this:

                       

                      public void processException(Message message, Throwable t)
                      {
                        System.out.println("Inside Exception");
                       
                        //Here how to build user defined xml that shows exception. I dont want exception coming as SOAPFault.
                       
                        /**
                         * User defined xml will include elements that we define
                         */
                        String message = ""
                                      + "<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">"
                                      + "  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">"
                        + "    <ProducerInScopeResponse>"
                        + "      <Exception>"
                        + "        Gone something bad!"
                        + "      </Exception>"
                        + "    </ProducerInScopeResponse>"
                        + "  </s:Body>"
                        + "</s:Envelope>
                      "

                        throw new ActionProcessingException(message);
                      }

                       

                      because your testcase does not contain any code to do this.

                       

                      Regards,

                       

                      Hans

                      • 8. Re: Throwing Custom exception .
                        madhucm

                        Yes i tried it ..... its not working either   !

                        • 9. Re: Throwing Custom exception .
                          h.wolffenbuttel

                          Sorry, I think you might want to Use ActionProcessingFaultException instead. It takes a message as a variable which should be used for the fault message:

                           

                          public Message throwException(Message message) throws ActionProcessingException

                             String errorMessage = ""

                                      + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"

                                      + "  <s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"

                                 + "    <ProducerInScopeResponse>"

                                 + "      <Exception>"

                                 + "        Gone something bad!"

                                 + "      </Exception>"

                                 + "    </ProducerInScopeResponse>"

                                 + "  </s:Body>"

                                 + "</s:Envelope>

                            if(message != null)

                            {

                             message.getBody().add(org.jboss.soa.esb.message.Body.DEFAULT_LOCATION,errorMessage);

                                    throw new ActionProcessingFaultException(message, "Error in action" );

                            }

                            return message;

                          }

                           

                           

                          regards,

                           

                          Hans

                          • 10. Re: Throwing Custom exception .
                            madhucm

                            It worked Hans,

                             

                            Thanks a lot

                            • 11. Re: Throwing Custom exception .
                              joe_boy12

                              Madhu and Hans - this was really helpful - thnx

                              I have a situation where I need to catch the exception lets say  java.net.UnknownHostException which gets thrown when my wise.SOAPClient action cannot download .wsdl while calling a SOAP service on other server. I have an action which has processException method implemented before it starts  the action chain indeed gets called after that above exception is thrown and I want to return a custom message to consumer and not HTTP 500 (as this is a synch call with http gateway listener), in this processException method I tried  editing the message body with custom XML message and throwing ActionProcessingFaultException from that - doesnt work. It simply returns http 500 to user instead of custom message.  The moment I force an action to throw ActionProcessingFaultException before my SOAPClient is called - it works perfect. do u know whats the problem? or how do we do it in such situations

                              • 12. Re: Throwing Custom exception .
                                h.wolffenbuttel

                                Joe_boy12,

                                 

                                Perhaps you should make a new thread because this one is already labelled solved. On what to do with exceptions, it depends. If you can't replace the original message in the exception, you will have to catch the exception yourself. This means writing somekind of wrapper around your SOAPClient which catches your exception. I have for example wrapped the process method of my httprouter to check the returned message. If its anything other than the expected format, I know something went wrong an can act on it. So you might want to try using httprouter instead of the SOAPClient if all fails.

                                 

                                Regards,

                                 

                                Hans

                                • 13. Re: Throwing Custom exception .
                                  joe_boy12

                                  Thanks Hans - will start a new thread.