Tuesday, December 1, 2015

How to use Fault Mediator in WSO2 ESB

When there is a requirement like to transform current message to fault message we can use fault mediator. This is the same as we have in throw exception in java. Fault mediator converts current message to fault message. You can send this message to client by using the send mediator. You can create the fault message as a SOAP 1.1, SOAP 1.2, or plain-old XML (POX) fault.

In below example I have created a same proxy which returns a transaction number but in IN sequence we are trying to implement fault message.



We have created FaultMediatorTest service and in IN sequence (FaultMediatorTest_IN) we will apply fault mediator. Go to edit sequence.




You can see Fault mediator comes inside the transform category because it is used to transform the message into fault message.


Once you click on Fault mediator you will get the above screen, in which you have to pass some parameter.

  • Version: There are only 2 versions in ESB 4.0.3 but in latest version you can see one more option POX.

  • Code: Basically it’s a fault code value you pass by selecting dropdown.
    • Version Mismatch: it shows the version mismatch of the SOAP
    • Must Understand: It’s a SOAP “Must understand” error, if SOAP is not there
    • Client: To show client side error.
    • Server: Server side error.
  • Value or Expression: If it’s a hard coded value you need to pass select Value or for variable you can select expression which you need to provide.
  • Fault String: detailed fault string, here we have chosen the ERROR_MESSAGE expression get error message.
  • Detail:  You can pass any message details which want to show.
  • ROLE: This option appear when you select version as SOAP1.2, you can provide SOAP role name.
  • Node: This option appears when you select version as SOAP1.2 you can provide SOAP Node name.


Synapse Code:
      <proxy name="FaultMediatorTest" transports="http" startOnLoad="true" trace="disable" statistics="enable">  
     <target inSequence="FaultMediatorTest_IN" faultSequence="CommonFaultHandler"/>  
     <publishWSDL key="FaultMediatorTest_WSDL"/>  
   </proxy>  
      <localEntry key="FaultMediatorTest_WSDL" src="file:repository/conf/makefault/resources/proxy/makefault.wsdl"/>  
   <sequence name="FaultMediatorTest_IN">  
     <makefault version="soap12">  
       <code xmlns:soap12Env="http://www.w3.org/2003/05/soap-envelope" value="soap12Env:MustUnderstand"/>  
       <reason xmlns:ns="http://org.apache.synapse/xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:ns3="http://org.apache.synapse/xsd" expression="get-property('ERROR_MESSAGE')"/>  
       <node/>  
       <role/>  
                <detail>This is a simple test for Fault Mediator</detail>  
     </makefault>  
     <send/>  
   </sequence>  



Result: