Skip to main content

Adding custom error messages within a custom sequence in API Manager


Overview

You can add custom error messages for an API by following the post [1] but when the error is thrown inside the custom in or out sequence we add as the mediation extension this custom error sequence which we specify for the API does not get invoked. Therefore we need to specify the error sequence seperately for the custom sequence. This blog will explain how to carry out this use case.

Let's get started

To demonstrate this I will be using the call mediator inside the custom sequence. Since when an invalid endpoint is invoked with the call mediator it would generate an error in the API Manager which would be received inside our custom sequence.

1) You can follow my previous tutorial [2] to get a better understanding and configurations needed to use the call mediator using wso2 api manager 1.10.0. Create the API as mentioned in the sequence above and subscribe and generate keys for the API in the store.

2) Now what we are going to do is, we are going to provide an invalid capital city. When the call mediator executes this since this is invalid and not found it would generate an error reponse in the swagger UI. This would execute the API Manager's default fault sequence.


3) Now since we need to change this default error code to display something more meaningful we need to create a custom error sequence. You can create a custom error sequence as instructed in the tutorial [1]. Below is the custom error sequence which I have used, you can download it from HERE.

       
<sequence xmlns="http://ws.apache.org/ns/synapse" name="customErrorSequence">           
    <payloadFactory>
        <format>
          <CapitalResponse>
                 <message>No result for the given capital: $1</message>
           </CapitalResponse>
        </format>
        <args>
             <arg expression="get-property('uri.var.capital')"/>
        </args>
    </payloadFactory>         
    <class name="org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler"/>
    <property name="RESPONSE" value="true"/>
    <property name="HTTP_SC" value="200" scope="axis2"/>
    <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="Host" scope="transport" action="remove"/>
    <property name="Accept" scope="transport" action="remove"/>
    <property name="X-JWT-Assertion" scope="transport" action="remove"/>
    <sequence key="_cors_request_handler_"/>
    <send/>
</sequence>

4) We need to then add this sequence it from the management console using the source view. Or you can add the sequence .xml file to the location APIM_HOME/repository/deployment/server/synapse-configs/default/sequences. After adding this change you may have to wait for 15 mins for changes to get reflected or else you can restart the API Manager.


5) Nest we need to edit the in sequence we used previously to point to this fault sequence when a fault occurs withing the in sequence. For that we need to modify the <sequence> tag to mentioned which is the sequence to be executed when an error occurs. The modified call mediator sequence is as follows and you can download it from HERE . 

<sequence xmlns="http://ws.apache.org/ns/synapse" name="callMediatorSampleSequence" onError="customErrorSequence">
<property name="uri.var.capital" expression="$url:capital"/> 
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
    <call blocking="true">
        <endpoint>
            <http method="get" uri-template="http://restcountries.eu/rest/v1/capital/{uri.var.capital}"/>
        </endpoint>
    </call>
<log level = "custom">
<property name = "Sequence name" value="In the call mediator sequence"/>
<property name = "Capital name" expression="get-property('uri.var.capital')"/>
<property name="JSONPayload" expression="json-eval($.)"/>
</log>
</sequence>

6) Next we need to add the above modifed sequence to our API and re publish the API. 

7) Now when we invoke the API with an invalid value for the call mediator we will be able to see our custom error message.



Refernces 


Comments

Popular posts from this blog

WSO2 Cloud

Introduction WSO2 Cloud consists of three commercial cloud offerings namely the API cloud, Integration Cloud and the Identity Cloud. WSO2 Integration Cloud WSO2 Integration Cloud is a public hosted version of the powerful, 100% open-source WSO2 Enterprise Integrator. It allows you to host your cloud-to-cloud, cloud-to-on-premise integrations and API backends on a scalable cloud platform WSO2 API Cloud WSO2 API Cloud is an enterprise-ready, self-service platform that enables you to expose business capabilities as managed APIs and leverage off-the-shelf potential for advertising and selling business APIs. The API Cloud supports all aspects of an API’s lifecycle, from its creation to publication and retirement. The API Cloud is based on WSO2 API Manager , WSO2’s complete solution for designing and publishing APIs and managing a developer community. [2] WSO2 Identity Cloud WSO2 Identity Cloud leverages the functionality of WSO2 Identity Server’s identity and ac...

Exposing a SOAP service as a REST API

In this post i will be explaining how we can transform a SOAP based backend to receive requests in a restful manner through the WSO2 API Cloud. Steps. First log into the WSO2 Cloud and navigate to the API Cloud. In the API cloud select the option to add a new API. We will be creating an API to demonstrate an invocation to the backend soap service ws.cdyne.com/phoneverify/phoneverify.asmx?wsdl Give a name, context and version to the API and add a resource name with a GET Method. The resource name can be anything which you like since we will invoke the actual service usiing a custom sequence. Mention the URI template as indicated in the below screenshot. Next go to the implement tab. And select the endpoint type as HTTP/SOAP Endpoint and specify the endpoint as http://ws.cdyne.com/phoneverify/phoneverify.asmx. There is an important step we need to do here. We need to set the SOAP version for this request. In order to do that we need to select the advanced option ...

Adding error sequence to a custom sequence

In my previous posi i explained how to call an endpoint using the call mediator. Usually when errors are thrown from the call mediator we can handle them using a custom sequence. When call mediators fails the execution stops hence handling errors is very essential. For this post I will be modifying the same sequence which i created in the blog post [1] The change which we need to do is we need to modify the sequence definition to map with the error sequence as shown below. You can find the modified sequence here . <sequence xmlns="http://ws.apache.org/ns/synapse" name="countrySequenceWithError" onError="countryErrorSequence"> <property name="uri.var.countryCode" expression="$url:countryCode"/> <property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>     <call blocking="true">         <endpoint>             <http method="get" uri-temp...