Saturday, December 2, 2017

Customize error response message - WSO2 API-Manager

Customize the response message when a token is invalid or expired.


In WSO2 API-Manager all failure messages will hit the auth failure handler. So that you can customize the response message by editing auth_failure_handler.xml file. You can find this XML file from <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences directory. 

  • When the token is invalid or expired, you will get 900901 error codes. So that you need to set it to the filter to filter out the error response message. 
<sequence name="_auth_failure_handler_" xmlns="http://ws.apache.org/ns/synapse">
    <property name="error_message_type" value="application/json"/>
    <filter source="get-property('ERROR_CODE')" regex="900901">
      <then>
          <sequence key="sample"/>
          <drop/>
      </then>
      <else>
      </else>
    </filter>
    <sequence key="_build_"/>
</sequence>

In above sequence filter condition remains the same message goes to then element. The messages that do not match the filter criteria message goes through else element. 

Then create a sample.xml file in the same directory.  
Add these configurations to sample.xml file.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="sample">
<payloadFactory media-type="json">
 <format>
     {
     "transaction_id": "ABC",
     "desc": "Success"
      }
 </format>
</payloadFactory>
<property name="RESPONSE" value="true" />
<header name="To" action="remove" />
<property name="NO_ENTITY_BODY" scope="axis2" action="remove" />
<property name="ContentType" scope="axis2" action="remove" />
<property name="Authorization" scope="transport" action="remove" />
<property name="Access-Control-Allow-Origin" value="*" scope="transport" />
<property name="Host" scope="transport" action="remove" />
<property name="Accept" scope="transport" action="remove" />
<property name="X-JWT-Assertion" scope="transport" action="remove" />
<property name="HTTP_SC" value="403" scope="axis2"/>
<property name="messageType" value="application/json" scope="axis2" />

<send/>
</sequence>

You can add your JSON response message under the <format> tag.

How to customize the response for specific APIs.

You can  customize the response to specific APIs  by adding two nested filters to <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences/auth_failure_handler.xml file.

  • Add API names which you want to keep the original XML response to the outer filter.
<sequence name="_auth_failure_handler_" xmlns="http://ws.apache.org/ns/synapse">
     <property name="error_message_type" value="application/xml"/>
    <filter source="$ctx:SYNAPSE_REST_API" regex="admin--Test2:v1.0 | admin--Test1:vv1.0">
        <then>
          <sequence key="_build_"/>
        </then>
        <else>
          <filter source="get-property('ERROR_CODE')" regex="900901">
            <then>
                <sequence key="sample"/>
                <drop/>
            </then>
            <else>
            </else>
          </filter>
        </else>
    <sequence key="_build_"/>
      </filter>
</sequence>

In above sequence, we can filter out specific APIs which we want to customize the response from API name.