简体   繁体   中英

How to customise an error message generated by the Anypoint API manager standard policy?

There is an API running on the CloudHub (Mule 4.4) and protected with the standard SLA Rate Limit policy.

We have a requirement to provide a custom error message body instead of the default one generated by the policy when authentication fails.

The default error body returned by the policy looks like this:

{
"error": "Invalid client id or secret"
}

When I run the application in debugger in Studio, it is intercepting the exception and I can see that the error type produced by the policy is CLIENT-ID-ENFORCEMENT:INVALID_CREDENTIALS.
This as well indicates that the exception is reaching the Mule application itself. So, it must be a way to override the default handler with the custom one.

First, I've tried adding the handler for error type to HTTP request processing flow beside the other API Kit errors. It did not intercept the error.

Then, I've defined a separate error handler for the error type and set it as a "Default Error Handler" in the global configuration.

<configuration doc:name="Configuration" doc:id="7a7e22cd-2ee7-42ee-af15-eaf2693d72d9" defaultErrorHandler-ref="apiErrorHandler" />
<error-handler name="apiErrorHandler">
<on-error-propagate enableNotifications="true" logException="true" type="CLIENT-ID-ENFORCEMENT:*">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unauthorised"}]]></ee:set-payload>
</ee:message>
</on-error-propagate>
</error-handler>

This didn't work as well.

Can you pls advise the right way to customise the error messages returned the standard SLA policy?

To be clear, I'm well aware that it is possible to code your own custom policy. But the intention is to keep the standard one and just customise the response for some error types.

This is not currently possible. Policies have to handle their own errors, they can not be intercepted in the flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM