Skip to content

Instantly share code, notes, and snippets.

@DaRiv94
Last active October 17, 2024 19:09
Show Gist options
  • Save DaRiv94/7966d72fa31fd7473957debf44f41244 to your computer and use it in GitHub Desktop.
Save DaRiv94/7966d72fa31fd7473957debf44f41244 to your computer and use it in GitHub Desktop.
Error Handling in Azure API Management
<set-header name="ErrorSource" exists-action="override">
<value>@(context.LastError.Source)</value>
</set-header>
<set-header name="ErrorReason" exists-action="override">
<value>@(context.LastError.Reason)</value>
</set-header>
<set-header name="ErrorMessage" exists-action="override">
<value>@(context.LastError.Message)</value>
</set-header>
<set-header name="ErrorSection" exists-action="override">
<value>@(context.LastError.Section)</value>
</set-header>
<set-header name="ErrorStatusCode" exists-action="override">
<value>@(context.Response.StatusCode.ToString())</value>
</set-header>
<set-header name="ErrorScope" exists-action="override">
<value>@(context.LastError.Scope)</value>
</set-header>
<set-header name="ErrorPath" exists-action="override">
<value>@(context.LastError.Path)</value>
</set-header>
<set-header name="ErrorPolicyId" exists-action="override">
<value>@(context.LastError.PolicyId)</value>
</set-header>
<choose>
<when condition="@(context.Response.StatusCode == 500)">
<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>Sorry, We had an error on our end</set-body>
</return-response>
</when>
</choose>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment