It is working for me by using below Policy definition:
Here, I have done for 200 , in Place 200, you can use 500:
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode >= 200)">
<send-one-way-request mode="new">
<set-url>https://microsoft.webhook.office.com/webhookb2/0196b69abe@72f1db47/IncomingWebhook/75d656c/c977881U1w1</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
try {
var payload = new {
@type = "MessageCard",
@context = "https://schema.org/extensions",
themeColor = "FF0000",
summary = "🚨 APIM 2xx Error Alert",
sections = new[] {
new {
activityTitle = "🔥 **APIM 2xx Error Detected**",
facts = new[] {
new { name = "Request Method", value = context.Request.Method },
new { name = "Request URL", value = context.Request.Url.Path + context.Request.Url.QueryString },
new { name = "Host", value = context.Request.Url.Host },
new { name = "Status Code", value = context.Response.StatusCode.ToString() },
new { name = "Status Reason", value = context.Response.StatusReason },
new { name = "Error Message", value = context.LastError?.Message ?? "N/A" },
new { name = "User Email", value = context.User?.Email ?? "N/A" }
},
markdown = true
}
}
};
return Newtonsoft.Json.JsonConvert.SerializeObject(payload);
} catch (Exception ex) {
return "{\"text\": \"⚠️ Failed to generate payload: " + ex.Message + "\"}";
}
}</set-body>
</send-one-way-request>
</when>
</choose>
<return-response>
<set-status code="200" reason="Simulated Error" />
<set-body>Testing Teams webhook.</set-body>
</return-response>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Added above policy in Inbound Processing:

Output:
Tested for 200:

APIM sends Message/Card in Teams Channel:

Try to follow above policy code and change the values and it will provide desired result as it has worked for me.
Hope this helps.
If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.