API / APIGEE Gateway Interview Questions
How does fault handling and error management work in Apigee?
Apigee provides a structured fault handling mechanism through FaultRules and DefaultFaultRule. When a policy raises an error, Apigee exits the normal flow and enters the error flow, where you can customise the error response returned to the client.
<!-- FaultRule: handle specific policy error --> <ProxyEndpoint name="default"> <FaultRules> <!-- Handle quota exceeded specifically --> <FaultRule name="QuotaExceeded"> <Condition>fault.name = "QuotaExceeded"</Condition> <Step><Name>AM.QuotaExceededResponse</Name></Step> </FaultRule> <!-- Handle invalid API key --> <FaultRule name="InvalidApiKey"> <Condition>fault.name = "InvalidApiKey"</Condition> <Step><Name>AM.UnauthorisedResponse</Name></Step> </FaultRule> </FaultRules> <!-- Catch-all for any unhandled fault --> <DefaultFaultRule name="DefaultFaultRule"> <Step><Name>AM.GenericErrorResponse</Name></Step> <AlwaysEnforce>true</AlwaysEnforce> </DefaultFaultRule> </ProxyEndpoint> <!-- Custom error response policy --> <AssignMessage name="AM.QuotaExceededResponse"> <AssignTo createNew="true" type="response"/> <Set> <StatusCode>429</StatusCode> <ReasonPhrase>Too Many Requests</ReasonPhrase> <Headers><Header name="Content-Type">application/json</Header></Headers> <Payload contentType="application/json">{ "error": "rate_limit_exceeded", "message": "You have exceeded your quota.", "retry_after": "3600" }</Payload> </Set> </AssignMessage> <!-- Key fault variables: fault.name -- e.g. "QuotaExceeded", "InvalidApiKey" fault.type -- "policy" or "messaging" error.message -- the error message string -->
More Related questions...