Mule 3.9中的处理异常和中断流

问题描述 投票:0回答:1

在Mule 3.9中,我确实遇到一个问题。我使用.this .raml文件生成了我的API。端点之一就是带有“ /”路由的GET(用于测试)。在此流程(管理GET /)中,我想调用另一个流程来检查某事(在我的情况下是JWT验证)。什么时候有效我想继续,并返回原始有效载荷。当无效时,我会在Groovy中调用一个确实发生的异常,但是毕竟,它涉及到相同的原始有效载荷,该异常不应可见。

让我们编写一些代码:

原流量:

        <flow-ref name="validate_jwt_token" doc:name="Validate JWT"/>

        <set-payload value=" { &quot;message&quot; : &quot;API is working!&quot;}" doc:name="Set Payload" mimeType="application/json" />
    </flow>

和流量参考:

<flow name="validate_jwt_token">
        <logger message="Request found with Authorization: #[message.inboundProperties['authorization']]" level="INFO" doc:name="Loger Authorization"/>
        <component doc:name="Validate JWT Token">
            <spring-object bean="ValidateJWTEvent"/>
        </component>
        <choice doc:name="Choice">
             <when expression="(payload == true)">
                <logger message="JWT valid. Access granted." level="INFO" doc:name="Logger"/>
            </when>
             <otherwise>

                <scripting:component doc:name="Throw Exception">
                    <scripting:script engine="Groovy"><![CDATA[throw new Exception('Invalid JWT Token.')]]></scripting:script>
                </scripting:component>
            </otherwise>
        </choice>
          <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-property propertyName="http.status" value="401" doc:name="Set 401 status"/>
            <flow-ref name="commons.exceptionStrategy.logError" doc:name="Log errors"/>
        </catch-exception-strategy>


    </flow>

即使此令牌实际上是无效的,我也将获得文本为“ API IS WORKING”的有效载荷。我想要做的是捕获异常并返回HTTP错误。仅此而已。

我也附上我的例外策略。请记住,它基于APIKIT,生成了我的Mule本身的.raml文件。

 <sub-flow name="commons.exceptionStrategy.logError">
        <logger message="Error: #[exception.?cause.message or exception]" level="ERROR" doc:name="Logger" />

        <set-property propertyName="Content-Type" value="application/json" doc:name="Set Content Type"/>
        <flow-ref name="log_params" doc:name="log_params"/>
        <logger message="#[org.apache.commons.lang3.StringEscapeUtils.escapeJson(exception.?getCauseException().?getMessage())]" level="ERROR" doc:name="Display error"/>
        <set-payload value="{ &quot;responseHeader&quot;: { &quot;requestId&quot;: &quot;#[flowVars.storedRequestId]&quot;, &quot;sendDate&quot;: &quot;#[function:now]&quot;}, &quot;code&quot;: &quot;#[flowVars.'statusCode']&quot;, &quot;message&quot;: &quot;#[flowVars.errorMessage or org.apache.commons.lang3.StringEscapeUtils.escapeJson(exception.?getCauseException().?getMessage())]&quot; }" doc:name="Set Error Message"/>
     </sub-flow>

 <sub-flow name="log_params">

<logger message="API| endpoint executed with payload: #[message.payload]" level="INFO" doc:name="log payload"/>
        <logger message="API| endpoint executed with flowVars: #[flowVars]" level="INFO" doc:name="log params"/>

        </sub-flow>

[APIKIT我的Mule通过全局捕获异常策略使用了整个API。

请帮助我。

exception mule mule-esb
1个回答
0
投票

看起来您正在以与抛出异常相同的流程捕获异常。因此,由于已处理异常,因此不会传播该消息,也不会中断该流。尝试改用rollback-exception-strategy,这将修改有效负载,但会将异常传播到调用方流ref并中断该流。 HTH

© www.soinside.com 2019 - 2024. All rights reserved.