如何自定义由Anypoint API Manager标准策略生成的错误消息?

发布于 2025-02-07 17:25:49 字数 1435 浏览 3 评论 0原文

sla速率限制策略。

我们需要提供自定义错误消息主体,而不是当身份验证失败时策略生成的默认错误消息主体。

该策略返回的默认错误正文看起来像这样:

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

当我在debugger中运行应用程序时,它正在拦截异常,我可以看到该策略产生的错误类型是客户端ID-neforcement:invalid_credentials。
这也表明例外已经达到了mule应用本身。因此,这必须是一种使用自定义的处理程序覆盖默认处理程序的方法。

首先,我尝试将错误类型添加到其他API套件错误旁边的HTTP请求处理流程中。它没有拦截错误。

然后,我为错误类型定义了一个单独的错误处理程序,并将其设置为全局配置中的“默认错误处理程序”。

<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>

这也不正常。

您可以建议定制错误消息返回标准SLA策略的正确方法吗?

需要明确的是,我很清楚可以编码您自己的自定义策略。但是目的是保留标准一个,只是自定义某些错误类型的响应。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2025-02-14 17:25:50

目前不可能。政策必须处理自己的错误,无法在流程中拦截。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文