How to customize the error message while using validate-content policy

Amy Zhang 40 Reputation points
2024-09-06T10:11:41.4433333+00:00

Hi, I want to know how to customize the error message while using validate-content policy.

The intention is to show the customized message when users input wrong field name or additional fields.

Schema used

{
  "type": "object",
  "properties": {
    "abc": {
      "type": "string"
    }
  },
  "required": [
    "MasterNo"
  ],
  "additionalProperties": false
}

So far the validate-content works, but the default error message is not clear.

 {
"error": {
    "code": "BadRequest",
    "message": "Invalid Request Body"
}
} 

So I want to customize the error message. Here's my attempt but it does not work, still return the default error message.

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="@("{{ERP-Cloud}}")" />
        <authentication-basic username="{{ERP-Cloud-Id}}" password="{{ERP-Cloud-Password}}" />
        <validate-content unspecified-content-type-action="ignore" max-size="500" size-exceeded-action="prevent" errors-variable-name="validationErrors">
            <content type="application/json" validate-as="json" action="prevent" schema-id="erp-check" schema-ref="#/components/schemas/erp-check" />
        </validate-content>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <choose>
            <when condition="@(context.Variables.ContainsKey("validationErrors"))">
                <set-header name="Content-Type" exists-action="override">
                    <value>application/json</value>
                </set-header>
                <set-body>@{
                var error = new JObject();
                error["code"] = "400";
                error["message"] = "The request body is invalid. Only the field 'MasterNo' is allowed.";
                return error.ToString();
            }</set-body>
            </when>
            <otherwise>
                <set-header name="Content-Type" exists-action="override">
                    <value>application/json</value>
                </set-header>
                <set-body>@{
                var error = new JObject();
                error["code"] = "500";
                error["message"] = "An unexpected error occurred.";
                return error.ToString();
            }</set-body>
            </otherwise>
        </choose>
    </on-error>
</policies>

Is there anything I get wrong ? Need your advice.

Thank your

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Bruno DEMEYERE 0 Reputation points
    2026-09-29T07:58:51.45+00:00

    I have the same trouble.
    It seems that the "on-error" policy is not "really" executed when you use "validate content" with action="prevent".
    I mean "on-error" is executed, but not the custom code you set in "on-error" section of your API (or in "API product").

    There is a kind of "shortcut" by APIM.

    Was this answer helpful?

    0 comments No comments

Your answer

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