Add Error handling in API policies
  • 23 Mar 2022
  • 1 Minute to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Add Error handling in API policies

  • Comment
  • Dark
    Light
  • PDF

In a previous API Management Tip, we discussed the use of policies for API operations. And one of the sections to add policies statements is on-error. With the on-error section, you can add several policy statements like log-to-eventhub, retry, and trace.

Azure API Management allows out-of-the-box publishers of APIs to respond to error conditions, which may occur during the processing of requests. The service provides a so-called ‘ProxyError’ object, which is accessible through the context.LastError property, which you can use within the on-error section.

When an error occurs during the processing of a request, and you have set policy statements in the on-error section, the error is stored in the context.LastError property. You can create a response to the caller of your API through the following policy:

1(3)

Note that API Management provides a few predefined errors for your APIs such as OperationNotFound and SubscriptionNotFound. Furthermore, there are predefined errors for policies like QuotaExceeded and CallerIpBlocked.

It makes sense to leverage the error handling capability for your policies and apply them on APIs and operations. For the consumer of your APIs it can be helpful and for operations with regards to troubleshooting. When designing your APIs consider using error handling capability, besides monitoring, versioning, and place them in a Product.

APIM.png


Was this article helpful?