Search code examples
exceptionapache-camelblueprint-osgiapache-servicemix

exception handling in apache servicemix


I am new to ServiceMix am trying on exception handling in ServiceMix.

when I try to print the exception, it also contains the body of the request in it. Is there any way that I can extract only the errors from exception?

<from uri="activemq:topic://topic1"/>
            <!-- Schema validationm for the request received from Biblio -->
            <doTry>
                <to uri="validator:http://localhost/employee.xsd"/>
                <doCatch>                                           
                    <exception>java.lang.Exception</exception>  
                    <log message="${exception.message}"/>           
                </doCatch>                  
            </doTry>

below is the logged exception:

org.apache.xerces.jaxp.validation.SimpleXMLSchema@a0059b5
errors: [
org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'asd' is not a valid value for 'integer'., Line : -1, Column : -1
org.xml.sax.SAXParseException: cvc-type.3.1.3: The value 'asd' of element 'empnumber' is not valid., Line : -1, Column : -1
]. Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>         
         <employee>
            <empnumber>asd</empnumber>            
            <surname>PM</surname>            
            <firstname>Abhinay</firstname>
         </employee>
      </empRecord>]

I am getting the correct exception but I dont want the below part in the exception.

Is there any way I can remove these from the exception message?

Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>         
         <employee>
            <empnumber>asd</empnumber>            
            <surname>PM</surname>            
            <firstname>Abhinay</firstname>
         </employee>
      </empRecord>]

Solution

  • That message is coming from the exception, so you can manipulate the string inside a Processor.

    Write a simple class to do this:

    private static final Logger logger_ = LoggerFactory
            .getLogger(ExceptionMsgProcessor.class);
    
    public void process(Exchange exchange) {
        Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        String msg = e.getMessage();
        // manipulate the string here
        log.info("Exception message: {}", msg);
    }
    

    And then send the Exception to this bean

    <doTry>
        <to uri="validator:http://localhost/employee.xsd"/>
        <doCatch>                                           
            <exception>java.lang.Exception</exception>
            <to uri="bean:exceptionMsgProcessor" />
        </doCatch>                  
    </doTry>