简体   繁体   中英

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

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.

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

curl -X GET http://localhost:8103/api/resource/8276 [{"errorCode":"401","errorMessage":"Authentication credentials are required."}]

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.

Normally using http:response-builder would overcome this.

See this 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 can sometimes cache authentication information from past requests and reuse that in new ones unexpectedly.

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."}]

Soap Bug information here: http://sourceforge.net/p/soapui/bugs/4/

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