简体   繁体   English

ule子异常处理未返回响应

[英]Mule exception handling not returning a response

I have a mule flow with a http:inbound-endpoint that uses an exchange-pattern of "request-response". 我有一个带有http:inbound-endpoint的m子流,该流使用“ request-response”的交换模式。

I am noticing something strange in my exception handling. 我在异常处理中注意到一些奇怪的事情。 I have two catch exception blocks inside a choice exception block, one that sets the http status to 500 and one to 401. Setting the http status to 500 causes a response to be sent back to the caller, but when I set the http status to 401, the flow continues to code that should not be hit in the case of that exception being caught. 我在选择异常块内有两个catch异常块,一个将http状态设置为500,一个将401设置为401。将http状态设置为500导致将响应发送回调用方,但是当我将http状态设置为401,该流程继续编码在捕获到该异常的情况下不应被击中的代码。

Below is my code : 下面是我的代码:

<choice-exception-strategy> <catch-exception-strategy doc:name="Catch UnauthorisedException Strategy"
            when="#[exception.causedBy(org.mule.api.security.UnauthorisedException)]">      
            <set-payload doc:name="Set Exception Message" value="Authentication credentials are required." />
            <set-property propertyName="http.status" value="401" doc:name="Set HTTP 401 Status" />
            <set-property propertyName="httpPath" value="#[message.inboundProperties.get('http.request.path')]" doc:name="Set httpPath"/>       
            <custom-transformer class="com.company.ErrorTransformer"
                doc:name="Transform Exception Message" />
            <json:object-to-json-transformer doc:name="Object to JSON" sourceClass="java.util.List" />
        </catch-exception-strategy> 
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload doc:name="Set Exception Message" value="A system error has occurred." />
            <set-property propertyName="http.status" value="500" doc:name="Set HTTP 500 Status" />
            <set-property propertyName="httpPath" value="#[message.inboundProperties.get('http.request.path')]" doc:name="Set httpPath"/>       
            <custom-transformer class="com.company.ErrorTransformer"
                doc:name="Transform Exception Message" />
            <json:object-to-json-transformer doc:name="Object to JSON" sourceClass="java.util.List" />
        </catch-exception-strategy>
  </choice-exception-strategy>

I attempted to use the information in the following post but it did not have any effect : stack overflow post 我试图使用信息在下面的文章,但它没有任何影响: 堆栈溢出后

When my code hits the 401 exception, it never does return that error response as I expect, though the other catch block with the 500 code DOES return the error response to the caller. 当我的代码遇到401异常时,它永远不会像我期望的那样返回错误响应,尽管另一个具有500代码的catch块确实将错误响应返回给调用者。 Any advice appreciated. 任何建议表示赞赏。

**Editing to add that this may have been a testing issue. **进行编辑以补充说这可能是一个测试问题。 I was seeing this behavior when testing the GET request via SOAPUI, however, when I try the request using CURL, it did behave as I would have expected : 通过SOAPUI测试GET请求时,我看到了这种现象,但是,当我尝试使用CURL进行请求时,它的行为确实与我期望的一样:

curl -X GET http://localhost:8103/api/resource/8276 [{"errorCode":"401","errorMessage":"Authentication credentials are required."}] curl -X GET http:// localhost:8103 / api / resource / 8276 [{“ errorCode”:“ 401”,“ errorMessage”:“需要身份验证凭据。”}]

The issue here, is actually quite simple, the HTTP transport when is producing the output is detecting that the current message contains an exception payload, when exception payload is different than null it will always produce a 500 error thus giving you the illusion of that part working. 这里的问题实际上很简单,HTTP传输在生成输出时会检测到当前消息包含异常有效负载,当异常有效负载不同于null ,它将始终产生500错误,从而使您对该部分有错觉工作。

Normally using http:response-builder would overcome this. 通常使用http:response-builder可以克服这个问题。

See this https://developer.mulesoft.com/docs/display/current/HTTP+Response+Builder 看到这个https://developer.mulesoft.com/docs/display/current/HTTP+Response+Builder

It turned out that my code was fine, this was actually a SOAPUI bug that was causing the behavior I saw. 原来,我的代码很好,这实际上是导致我所看到的行为的SOAPUI错误。 SOAPUI can sometimes cache authentication information from past requests and reuse that in new ones unexpectedly. SOAPUI有时可以缓存来自过去请求的身份验证信息,并在新请求中意外地重用它们。

When I retested this using curl, the code behaved as excpected: curl -X GET http://localhost:8103/api/resource/8276 [{"errorCode":"401","errorMessage":"Authentication credentials are required."}] 当我使用curl重新测试它时,代码表现出预期的效果:curl -X GET http:// localhost:8103 / api / resource / 8276 [{“ errorCode”:“ 401”,“ errorMessage”:“需要身份验证凭据。 “}]

Soap Bug information here: http://sourceforge.net/p/soapui/bugs/4/ 此处的Soap Bug信息: http : //sourceforge.net/p/soapui/bugs/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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