簡體   English   中英

ule子異常處理未返回響應

[英]Mule exception handling not returning a response

我有一個帶有http:inbound-endpoint的m子流,該流使用“ request-response”的交換模式。

我在異常處理中注意到一些奇怪的事情。 我在選擇異常塊內有兩個catch異常塊,一個將http狀態設置為500,一個將401設置為401。將http狀態設置為500導致將響應發送回調用方,但是當我將http狀態設置為401,該流程繼續編碼在捕獲到該異常的情況下不應被擊中的代碼。

下面是我的代碼:

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

我試圖使用信息在下面的文章,但它沒有任何影響: 堆棧溢出后

當我的代碼遇到401異常時,它永遠不會像我期望的那樣返回錯誤響應,盡管另一個具有500代碼的catch塊確實將錯誤響應返回給調用者。 任何建議表示贊賞。

**進行編輯以補充說這可能是一個測試問題。 通過SOAPUI測試GET請求時,我看到了這種現象,但是,當我嘗試使用CURL進行請求時,它的行為確實與我期望的一樣:

curl -X GET http:// localhost:8103 / api / resource / 8276 [{“ errorCode”:“ 401”,“ errorMessage”:“需要身份驗證憑據。”}]

這里的問題實際上很簡單,HTTP傳輸在生成輸出時會檢測到當前消息包含異常有效負載,當異常有效負載不同於null ,它將始終產生500錯誤,從而使您對該部分有錯覺工作。

通常使用http:response-builder可以克服這個問題。

看到這個https://developer.mulesoft.com/docs/display/current/HTTP+Response+Builder

原來,我的代碼很好,這實際上是導致我所看到的行為的SOAPUI錯誤。 SOAPUI有時可以緩存來自過去請求的身份驗證信息,並在新請求中意外地重用它們。

當我使用curl重新測試它時,代碼表現出預期的效果:curl -X GET http:// localhost:8103 / api / resource / 8276 [{“ errorCode”:“ 401”,“ errorMessage”:“需要身份驗證憑據。 “}]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM