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