Search code examples
wso2wso2-data-services-serverwso2-esb

Custom error messages and HTTP return codes in WSO2DSS


I am using Wso2 DSS and building RESTful webservices from an oracle DB and trying to return custom error messages in case no records found or invoked using invalid parameters. And I also want to control the HTTP return code for these error responses.

For example. Lets say there is a webservice: /getemployee/{empno} that returns employee details in JSON/XML format.

If I call getemployee with an empno that doesn't exists in the DB then

  1. I want the response to be like something similar to below

    <Error>

    <Error_code>No employee found </Error_code>

    </Error>

It should not have the normal employee details message structure that would go as response when an employee is found. At the moment if record is not found by the underlying query DSS just returns empty message with just root element.

  1. The HTTP return code for this response should be 404 as opposed to the success code 200. By default all responses get HTTP return code 200 in DSS.

Is this possible at all just by using DSS?

Appreciate your help


Solution

  • You would need WSO2 ESB to do that. You can make a proxy service in ESB that will call DSS and then transform the response for you based on its contents and your needs. You can do so using XSLT.

    You can set manually the desired HTTP return code, using the property HTTP_SC:

    <property name="HTTP_SC" scope="axis2" type="STRING" value="404"/>
    

    You can also use switch and payload factory, if you know what you will receive and the output will be a static one.