Over a million developers have joined DZone.

WSO2 ESB Endpoints Error Handling

· Integration Zone

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

This post will illustrate error handling in wso2 esb in regards to Endpoints. It's an import topic to cover because WSO2 Enterprise Service Bus needs to send the message to a service provider in its final stage.

image

Usecase One:

WSO2 ESB sample proxy is pointing in to End point that can be time out. Proxy must able give nice valid message for to user regard to the is time out rather than false message. Response message is able to customize depending on the Error Code.

1. Create End Point (Web services that will make time out ) and host it. (I am using wso2 AS for to host time out services)

2. Create Proxy that end point

<proxy xmlns="http://ws.apache.org/ns/synapse" name="TimeoutProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
 

<target faultSequence="fault">
      <outSequence>
         <log>
            <property name="Out Sequence Testing " value="=============In Out Sequence============"/>

         </log>
         <send/>
      </outSequence>
    

    <endpoint>
         <address uri="http://localhsot:9763/services/TimeoutService/">
            <timeout>
               <duration>1000</duration>
               <responseAction>discard</responseAction>
            </timeout>
         </address>
      </endpoint>


   </target>
   <description></description>
</proxy>

3. Go to "Home > Manage > Service Bus > Sequences" and Click  on fault Sequence Edit link

image

4. Update it with below code

<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault">

   <property xmlns:ns="http://org.apache.synapse/xsd" name="ErrorCode" expression="get-property('ERROR_CODE')" scope="default" type="INTEGER"/>

   <log level="custom">
      <property name="failS" value="=======False Sequence==========="/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="testCode" expression="$ctx:ErrorCode"/>
   </log>

   <switch xmlns:ns="http://org.apache.synapse/xsd" source="$ctx:ErrorCode">
      <case regex="303001">
         <log>
            <property name="go" value="==== 303001 ==== Error msg transformation"/>
         </log>
      </case>
      <case regex="101503">
         <log>
            <property name="go" value="==== 101503 ==== Error msg transformation"/>
         </log>
      </case>
   </switch>

<drop/>

</sequence >

image

NOTE

Transport Error Codes

Transport Error Codes WSO2 ESB 4.6.0

Endpoint Failures

Endpoint Failures

Here is console of wso2 ESB

image

5. You can do switch depending on your Error Code or Action

<property xmlns:ns="http://org.apache.synapse/xsd" name="Action" expression="get-property('Action')"/>

Or

<property xmlns:ns="http://org.apache.synapse/xsd" name="ErrorCode" expression="get-property('ERROR_CODE')" scope="default" type="INTEGER"/>

6. Rather Logging We need to get Respond Message where user can understand what was happen.

Here is sample Msg for that Error code 101503

<getBusRootResponse xmlns="">
            <error>
               <errorCode>101503</errorCode>
                <errorMsgTitle>Network Connection Failure</errorMsgTitle>
               <errorMsg>We are unable to get Connection to BusRoot Services</errorMsg>
            </error>
         </getBusRootResponse>

Add payload for each case and build your message or add property each case have constracted message details to be show

7. Remove Header “TO”

8. Add  Property "RESPONSE" as  "true"

9. Replace Drop with Send

image

Here is fault sequence

<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault">
   <log level="full"/>
   <property xmlns:ns="http://org.apache.synapse/xsd" name="ErrorCode" expression="get-property('ERROR_CODE')" scope="default" type="INTEGER"/>
   <log level="custom">
      <property name="failS" value="=======False Sequence==========="/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="testCode" expression="$ctx:ErrorCode"/>
      <property xmlns:ns="http://org.apache.synapse/xsd" name="Action" expression="get-property('Action')"/>
   </log>
   <switch xmlns:ns="http://org.apache.synapse/xsd" source="$ctx:ErrorCode">
      <case regex="303001">
         <log>
            <property name="go" value="==== 303001 ==== Error msg transformation"/>
         </log>
        <payloadFactory>
            <format>
               <getBusRootResponse xmlns="">
                  <error>
                     <errorCode>301503</errorCode>
                     <errorMsgTitle>EndPoint Connection Failure</errorMsgTitle>
                     <errorMsg>We are unable to get Connection to BusRoot Services Provider</errorMsg>
                  </error>
               </getBusRootResponse>
            </format>
         </payloadFactory>
      </case>
      <case regex="101503">
         <log>
            <property name="go" value="==== 101503 ==== Error msg transformation"/>
         </log>
        <payloadFactory>
            <format>
               <getBusRootResponse xmlns="">
                  <error>
                     <errorCode>101503</errorCode>
                     <errorMsgTitle>Network Connection Failure</errorMsgTitle>
                     <errorMsg>We are unable to get Connection to BusRoot Services</errorMsg>
                  </error>
               </getBusRootResponse>
            </format>
         </payloadFactory>
      </case>
   </switch>
  <header name="To" action="remove"/>
   <property name="RESPONSE" value="true"/>
   <send/>
</sequence>

Here is Out from Try it

image

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

Topics:

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}