Proper Error Handling in API Management

Edward Hakin 5 Reputation points
2025-07-29T19:07:50.1333333+00:00

I need this section of documentation explained:

https://learn.microsoft.com/en-us/azure/api-management/api-management-error-handling-policies#error-handling-in-api-management During the processing of a request, built-in steps are executed along with any policies, which are in scope for the request. If an error occurs, processing immediately jumps to the on-error policy section. The on-error policy section can be used at any scope. API publishers can configure custom behavior such as logging the error to event hubs or creating a new response to return to the caller.

Is this tracking policy errors, or is this tracking APIM errors, or is it tracking both? I have a load balanced OpenAI solution, with APIM circuit breaker. Within the backend section, in the retry logic, if the status code is == 429, a new variable is created that stores the backend responseBody. In the on-error section, I can force a 429 if that variable exists (using set-status code and set-body). My app can then use this response to retry calls to OpenAI during a cool off period.

My issue now relates to all other 400x errors. These do not seem to trigger on-error at all. A 401 returned, will log to event-hub in the outbound section (which seems to indicate the on-error is never triggered). The 401 will not be logged in the on-error section. Similar behavior is observed with 404 and other error codes.

Currently my policy is:

<inbound>

<backend>

<outbound>

<on-error>

What is the proper use for this? Our implementation of circuit breaker was obfuscating actual error responses, which is why I'm trying to preserve the original error body.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.