Abort processing policy
Aborting the processing
The ability to terminate a response gracefully is of importance in a number of cases such as error handling or business logic. Using the return-response policies short-circuits the request and yields a response that often does not originate from the backend. Consider what general situations may make sense without shifting too much business logic into APIM.
- Open the Finds Pets by tags operation in the Swagger Petstore API. This is a deprecated operation, making it ideal for demonstrating how to return a custom error response.
- Feel free to test it first, use a tag of ‘photo’ or similar - you should get an empty response back.
- Open the Code View.
- Add the inbound policy to test for a condition (just
truefor our example) and return an error. - Invoke the API.
-
Observe the 500 error.
<inbound> <base /> <choose> <when condition="@(true)"> <return-response response-variable-name="existing response variable"> <set-status code="500" reason="Internal Server Error" /> <set-header name="failure" exists-action="override"> <value>failure</value> </set-header> <set-body>Internal Server Error</set-body> </return-response> </when> </choose> </inbound>
Clean Up
Now that you have seen how to gracefully terminate a request with a response, it is time to clean up the code to prevent a downstream impact in subsequent labs. Please remove the
<choose>logic above to let all requests flow again, then save the changes.