Handling Target Errors
This example demonstrates the way errors from target servers can be handled and customized using the Rules and Fault Handling policy. When the backend API which returns JSON sends an HTML page when the resource is not available, the client JSON parsers could fail to interpret the responses. Using Fault Handling policy, the response can be customized so that a 500 (Internal Server Error) is sent in predefined JSON format.
Use Case
Policies used in the Use Case
Fault Handling Policy - The error response is customized using this policy.
Creating the Project
Create a project from a REST/HTTP service as defined in the Adding API project section.
Steps to perform Error Handling
Adding the Policy
Fault Handling Policy needs to be added as explained in the Adding a Policy section.
Assigning Rule
A rule needs to be assigned to the policy so that the policy gets executed on certain conditions. Error codes 404 (Not Found) and 500 (Internal Error) are used in this example. Since these errors are due to issues with the back end APIs and cannot be controlled by the API user, an appropriate error message is sent to the client reporting the issue.
Customizing the Error
The error is customized so that the 404 HTML page returned by backend server is replaced by a JSON response. An HTTP 500 response code corresponds to the internal error at the API Proxy Server. The status code, status phrase, and the message to be sent back to the client can be customized as part of the Fault Handling Policy configuration as shown below.