Monday, January 19, 2015

WSO2ESB ERROR_HANDLING Another way

To handle errors that occurred in ESB there are inbuilt mediators but from those mediators I cannot catch some expectations like XSLT transformation exception.
For achieve that I use following Hack in sequence. actually this sequence to execute when error occurred in IN sequence.
 <inSequence onError="ErrorSequence">

In error sequence Error message and Error Code can capture. unfortunately ESB returns limited error codes for errors so to catch error ERROR_CODE is not a option. 
ERROR_MESSAGE can be use with filter mediator as follow.

    <filter xmlns:ns="http://org.apache.synapse/xsd" source="get-property('ERROR_MESSAGE')" regex=".*Unable to perform XSLT transformation.*">
<then></then>

</filter>

can be use to regex to detect error messsage and send  response as error to client

complete filter :


 <filter xmlns:ns="http://org.apache.synapse/xsd" source="get-property('ERROR_MESSAGE')" regex=".*Unable to perform XSLT transformation.*">
      <then>
         <payloadFactory media-type="json">
            <format>  "ERROR" : {             "ERORR" : "Error in SXLT Transfotmation"            "ERORR_MESSAGE" : $1                          }  </format>
            <args>
               <arg expression="get-property('ERROR_MESSAGE')" evaluator="xml"></arg>
            </args>
         </payloadFactory>
         <header name="To" action="remove"></header>
         <property name="NO_ENTITY_BODY" action="remove" scope="axis2"></property>
         <property name="RESPONSE" value="true"></property>
         <property name="messageType" value="application/json" scope="axis2"></property>
         <send></send>
      </then>
      <else>
         <payloadFactory media-type="json">
            <format>                            "ERROR" : {                                "ERORR_MESSAGE" : $1                           }            </format>
            <args>
               <arg expression="get-property('ERROR_MESSAGE')" evaluator="xml"></arg>
            </args>
         </payloadFactory>
         <header name="To" action="remove"></header>
         <property name="NO_ENTITY_BODY" action="remove" scope="axis2"></property>
         <property name="RESPONSE" value="true"></property>
         <property name="messageType" value="application/json" scope="axis2"></property>
         <send></send>
      </else>
   </filter>

No comments:

Post a Comment