mule - Exception not being caught in mulesoft -


i working on app , in case exception occurs prints exception message appropriately.here custom filter

    package filter; import java.io.ioexception; import java.util.map; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import org.mule.api.mulemessage; public class exceptionclass implements org.mule.api.routing.filter.filter {      @override     public boolean accept(mulemessage message) {         map<string,object> payload=(map<string,object>)message.getpayload();          if(!payload.containskey("productid"))         {              throw new java.lang.nullpointerexception("no data found in payload "+payload.tostring());          }         if(integer.parseint((string) payload.get("productid"))<5)         {         throw new illegalargumentexception("invalid input payload " +payload.tostring());          }         return true;     }  } 

here configuration of app

    <?xml version="1.0" encoding="utf-8"?>  <mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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"      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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">     <http:listener-config name="http_listener_configuration" host="0.0.0.0" port="8081" doc:name="http listener configuration"/>     <flow name="muleexceptionhandlingflow">         <http:listener config-ref="http_listener_configuration" path="/" doc:name="http" >             <http:response-builder statuscode="#[flowvars['statuscode']]" reasonphrase="#[flowvars['reason']]"/>         </http:listener>         <json:json-to-object-transformer returnclass="java.util.hashmap" doc:name="json object"/>         <custom-filter doc:name="custom" class="filter.exceptionclass"/>         <logger message="payload:#[payload]" level="info" doc:name="logger"/>         <logger message="#[message]" level="info" doc:name="logger"/>         <set-payload value="payload:#[payload]" doc:name="set payload"/>     </flow>     <choice-exception-strategy name="muleexceptionhandlingchoice_exception_strategy">         <catch-exception-strategy doc:name="catch missing data exception strategy" logexception="false" when="#[exception.causedby(java.lang.nullpointerexception)]">             <set-payload value="missing data:#[payload]" doc:name="set payload"/>             <set-variable variablename="reason" value="missing input data" doc:name="variable"/>             <set-variable variablename="statuscode" value="400" doc:name="variable"/>         </catch-exception-strategy>         <catch-exception-strategy doc:name="catch invalid data exception strategy" logexception="false" when="#[exception.causedby(java.lang.illegalargumentexception)}">             <set-payload value="invalid data:#[payload]" doc:name="set payload"/>             <set-variable variablename="reason" value="invalid input" doc:name="variable"/>             <set-variable variablename="statuscode" value="400" doc:name="variable"/>         </catch-exception-strategy>     </choice-exception-strategy> </mule> 

and here error message in case of nullpointer exception

error 2017-07-25 17:35:49,595 [[muleexceptionhandling].http_listener_configuration.worker.01] org.mule.exception.defaultmessagingexceptionstrategy: ******************************************************************************** message : no data found in payload {price=1000, productname=shampoo} (java.lang.nullpointerexception). payload
: {price=1000, productname=shampoo} payload type : java.util.hashmap filter : filter.exceptionclass@584d4f19 element : /muleexceptionhandlingflow/processors/1 @ muleexceptionhandling:null:null -------------------------------------------------------------------------------- root exception stack trace: java.lang.nullpointerexception: no data found in payload {price=1000, productname=shampoo} @ filter.exceptionclass.accept(exceptionclass.java:23) @ org.mule.routing.messagefilter.accept(messagefilter.java:93) @ org.mule.processor.abstractfilteringmessageprocessor.process(abstractfilteringme ..................................

why exception not getting caught exception handler

please guide!

the reason exception strategy not getting called because have exception strategy outside block. update below:

      <flow name="muleexceptionhandlingflow">             <http:listener config-ref="http_listener_configuration" path="/" doc:name="http" >                 <http:response-builder statuscode="#[flowvars['statuscode']]" reasonphrase="#[flowvars['reason']]"/>             </http:listener>             <json:json-to-object-transformer returnclass="java.util.hashmap" doc:name="json object"/>             <custom-filter doc:name="custom" class="filter.exceptionclass"/>             <logger message="payload:#[payload]" level="info" doc:name="logger"/>             <logger message="#[message]" level="info" doc:name="logger"/>             <set-payload value="payload:#[payload]" doc:name="set payload"/> <choice-exception-strategy name="muleexceptionhandlingchoice_exception_strategy">             <catch-exception-strategy doc:name="catch missing data exception strategy" logexception="false" when="#[exception.causedby(java.lang.nullpointerexception)]">                 <set-payload value="missing data:#[payload]" doc:name="set payload"/>                 <set-variable variablename="reason" value="missing input data" doc:name="variable"/>                 <set-variable variablename="statuscode" value="400" doc:name="variable"/>             </catch-exception-strategy>             <catch-exception-strategy doc:name="catch invalid data exception strategy" logexception="false" when="#[exception.causedby(java.lang.illegalargumentexception)}">                 <set-payload value="invalid data:#[payload]" doc:name="set payload"/>                 <set-variable variablename="reason" value="invalid input" doc:name="variable"/>                 <set-variable variablename="statuscode" value="400" doc:name="variable"/>             </catch-exception-strategy>         </choice-exception-strategy>         </flow> 

i reading through blogs use-case , struck me though code work , give result expected, filters have been designed stop flow if returns false , design not throw exception. have 2 cleaner options handle scenario. (i) use validation component, mule intends use if want perform input validations.

so example, validation rule -

<validation:is-true config-ref="validation_configuration" expression="#[payload.containskey('productid')]" message="payload doesnt have productid" doc:name="validation"/> 

more interestingly, can put multiple validations in same component using option:- like

<validation:all config-ref="validation_configuration"   doc:name="validation">             <validation:validations>                 <validation:is-true expression="#[integer.parseint((string) payload.get(&quot;productid&quot;))&lt;5]" message="prod id less 5"/>                 <validation:is-true expression="#[payload.containskey('productid')]" message="no product id"/>             </validation:validations>         </validation:all> 

if explore component more, find ways catch validation exceptions using custom exceptions or using message string logged part of exception message.

(ii) use filter have used, wrap around message-filter component explained in below blog: https://www.ricston.com/blog/playing-with-mule-filters/ in example, can achieve exception below expression filter:

<message-filter doc:name="message" throwonunaccepted="true">         <expression-filter expression="#[payload.containskey(&quot;productid&quot;)]" doc:name="expression"/>         </message-filter> 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -