In the last blog, we discussed some basics of Apigee. If you haven’t created your proxies yet then, read the blog Apigee101: Constructing your own Proxy and create one. Now, let’s discuss how we can handle faults through Apigee which can’t be handled by our services but first we should know what Fault Handling is. So, Let’s get started
- Many error conditions can arise while API proxies are servicing requests from apps. For example, API proxies might encounter network issues when communicating with backend services.
- When an error occurs after a client app calls an API proxy, an error message gets returned to the client.
- By default, the client receives an often cryptic error message with no details or guidance.
- But if you want to replace default error messages with more useful custom messages, and even enrich them with things like additional HTTP headers, you need to set up custom fault handling in Edge.
What API proxies do when an error occurs
- Exit the proxy pipeline – When an API proxy encounters an error, regardless of how it occurs, it exits the normal flow pipeline, enters an error state, and returns an error message to the client app.
- Check for FaultRules – In the error state, API proxies also check for the presence of the following (in order) in the API proxy configuration before returning a default error message to the client app:
- A section, which contains the logic to trigger custom error messages (and other policies) based on specific conditions that you define.
- A section, which triggers a default error message in the following situations:
- No is defined.
- No existing get executed.
- The element is set to true.
It’s time to create fault rules but before that, you need to ensure that you have constructed your own Apigee proxy. Please refer this blog on Apigee 101: Constructing your own Proxy if you haven’t constructed your Proxy yet.
You can see your proxies from the Dashboard:
Here in the above snapshot, I have created NewProxy as my proxy for handling faults.
Simple Fault Handling Example
- Click on your proxy and you will be switched to overview of proxies.
- Select the Develop tab present at the top right corner next to the Overview tab.
- Now, you can add up the policies to the proxy you have created by clicking on +(Add New Policy).
- You will be pop-up with Add policy where you can select the policy you want.
- Let’s add Spike Arrest policy to limit the number of requests per minute. Change your 30ps to 1pm to allow only one request to come up per minute.
- Add the policy Spike Arrest-1 to your Proxy Endpoint preflow:
- Now, navigate to Overview tab again and copy the Deployments URL and hit in your browser. If you hit more than once in a minute you will get a SpikeArrestVoilation error. It may be that after hitting one time your response is not modified. Hence, in that case, hit until you get the error below:
- But we want our custom message to show up to the user in a JSON format which explains the error. Thus, we will add policy for handling the SpikeArrestVoilation error.
- Let’s add the Assign Message policy to handle the error.
- Add the code that is displayed in the snippet below to the Assign Message policy wherein Payload is defined as JSON containing Info “Only 1 per minute request is allowed” which will be shown to a user when the error will occur:
- Now, attach the Rate Limit Message Policy to handle SpikeArrestVoilation error.
- Let’s hit the Deployment URL that we hit earlier on the browser to see the JSON custom error we have implemented. Again, hit until you see the error.
This is how you can handle errors on Apigee and can show up the custom error message to the user.
Hope you liked the blog!! Stay Tuned 🙂