nits
nits

Reputation: 31

WSO2 API manager - How to send Error/Fault message back to the Client from InSequence

I have created a REST API using WSO2 API Manager (StockQuoteService) and configured a back end SOAP based web service (converting REST to SOAP) from where it will be getting the data based on the URL template.

In the "In Sequence", I have used a Switch mediator to send the request to different back end endpoints based on incoming data whereas in the default scenario (when no case match), I want to send the error message back to the client that the "Input message is invalid".

I have tried using the Send mediator, Respond Mediator, Sequence Mediator but still no success (may be doing something wrong) as still I am getting "no response from server" error when I try to invoke the URL which doesn't match any case of Switch and goes to Default.

How can I send the Error/Fault message back to the client from In Sequence of WSO2 API Manager?

Upvotes: 3

Views: 1320

Answers (3)

alber arce
alber arce

Reputation: 371

Nits The error response as comments "no response from server", I receive it when I call the api from the test console that has the API Manager.

You can see this link API Console Issue

To validate the response you expect.

Try calling from another tool such as SOAP UI or Postman, in my case I receive a fault response message with the structure defined.

This is the call from the console API

curl -X GET --header 'Accept: application/xml' --header 'Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b' 'https://169.254.193.10:8252/getById/1.0.0/getEmployeeXML?Id=5'

For the call from another client in the header pass these values you have in the api call

Accept: application/json
Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b'

Upvotes: 0

alber arce
alber arce

Reputation: 371

In my scenario, the input sequence I used the switch mediator and I invoke an operation or another, in the default option I create my failure response

  <inSequence>
     <switch xmlns:xsd="http://pharmacy.arce.org/xsd"
             description=""
             source="//xsd:desc">
        <case regex="NATURAL">
           <log description="Search Pharmacy" level="custom" separator=",">
              <property name="STATUS" value="Search Pharmacy"/>
           </log>
           <payloadFactory media-type="xml">
              <format>
                 <p:searchpharmacy xmlns:p="http://pharmacy.arce.org">
                    <ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
                       <xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
                       <xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
                       <xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
                       <xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
                    </ax22:pharmacy>
                 </p:searchpharmacy>
              </format>
              <args>
                 <arg evaluator="xml" expression="//xsd:desc"/>
                 <arg evaluator="xml" expression="//xsd:id"/>
                 <arg evaluator="xml" expression="//xsd:latitude"/>
                 <arg evaluator="xml" expression="//xsd:longitude"/>
              </args>
           </payloadFactory>
           <header name="To" scope="default" value="urn:searchpharmacy"/>
           <log level="full" separator=",">
              <property name="Mensaje" value="Cuerpo"/>
           </log>
        </case>
        <case regex="EXPERIMENTAL">
           <log description="Search Pharmacy Direction" level="custom" separator=",">
              <property name="STATUS" value="Search Pharmacy Direction Request"/>
           </log>
           <payloadFactory media-type="xml">
              <format>
                 <p:searchPhone xmlns:p="http://pharmacy.arce.org">
                    <ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
                       <xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
                       <xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
                       <xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
                       <xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
                    </ax22:pharmacy>
                 </p:searchPhone>
              </format>
              <args>
                 <arg evaluator="xml" expression="//xsd:desc"/>
                 <arg evaluator="xml" expression="//xsd:id"/>
                 <arg evaluator="xml" expression="//xsd:latitude"/>
                 <arg evaluator="xml" expression="//xsd:longitude"/>
              </args>
           </payloadFactory>
           <header name="Action" scope="default" value="urn:searchPhone"/>
           <property name="SOAPAction" scope="transport" type="STRING" value=""/>
           <log level="full" separator=",">
              <property name="Data" value="Body"/>
           </log>
        </case>
        <default>
           <log description="Fault" level="custom" separator=",">
              <property name="STATUS" value="Invoke fault "/>
           </log>
           <payloadFactory media-type="xml">
              <format>
                 <rs:fault xmlns:rs="http://pharmacy.arce.org">
                    <rs:code>-1</rs:code>
                    <rs:type>Invocation error</rs:type>
                    <rs:message>No operation has been invoked</rs:message>
                    <rs:description>The value of the input parameter is not valid</rs:description>
                 </rs:fault>
              </format>
              <args/>
           </payloadFactory>
           <respond/>
        </default>
     </switch>
  </inSequence>

Here is the default section

<default>
   <log description="Fault" level="custom" separator=",">
      <property name="STATUS" value="Invoke fault "/>
   </log>
   <payloadFactory media-type="xml">
      <format>
         <rs:fault xmlns:rs="http://pharmacy.arce.org">
            <rs:code>-1</rs:code>
            <rs:type>Invocation error</rs:type>
            <rs:message>No operation has been invoked</rs:message>
            <rs:description>The value of the input parameter is not valid</rs:description>
         </rs:fault>
      </format>
      <args/>
   </payloadFactory>
   <respond/>
</default>

And the fault response

  {"fault":{"code":-1,"type":"Invocation error","message":"No operation has been invoked","description":"The value of the input parameter is not valid"}}

You can find other scenarios here

http://harshcreationz.blogspot.com/2016/02/common-and-error-handling-sequences.html

Upvotes: 2

Bee
Bee

Reputation: 12513

This should work.

<payloadFactory media-type="json">
      <format>
          {
            "error":"true",
            "message":"error case"
          }
      </format>  
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<respond/>

Upvotes: 0

Related Questions