I need to set Status code and Reason Phrase in mule HTTP using Mule Choice exception strategy under that is Catch exception strategy. Following the Mulesoft documentation https://docs.mulesoft.com/mule-user-guide/v/3.7/http-listener-connector#http-response-status-code-and-reason-phrase
I tried doing something like this :
<choice-exception-strategy name="MyExptn">
<rollback-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy">
<set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/>
<set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/>
<add-message-property key="messageID" value="#[message.id]"/>
<add-message-property key="status" value="Error"/>
<add-message-property key="executionPoint" value="Error Handling"/>
<add-message-property key="ip" value="#[server.ip]"/>
<add-message-property key="serverName" value="#[server.host]"/>
<add-message-property key="domainname" value="xyz.com"/>
<add-message-property key="errorCode" value="123"/>
<add-message-property key="errorType" value="Bad request"/>
<add-message-property key="errorText" value="ComponentException"/>
<add-message-property key="integrationName" value="${integrationName}"/>
<add-message-property key="resourceType" value="HTTP Request"/>
<add-message-property key="resourceName" value="Payload"/>
</message-properties-transformer>
<logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/>
</rollback-exception-strategy>
</choice-exception-strategy>
Which is working fine. But when I'm replacing it with Catch Exception Strategy as shown below :
<choice-exception-strategy name="MyExptn">
<catch-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy">
<set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/>
<set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/>
<add-message-property key="messageID" value="#[message.id]"/>
<add-message-property key="status" value="Error"/>
<add-message-property key="executionPoint" value="Error Handling"/>
<add-message-property key="ip" value="#[server.ip]"/>
<add-message-property key="serverName" value="#[server.host]"/>
<add-message-property key="domainname" value="xyz.com"/>
<add-message-property key="errorCode" value="123"/>
<add-message-property key="errorType" value="Bad request"/>
<add-message-property key="errorText" value="ComponentException"/>
<add-message-property key="integrationName" value="${integrationName}"/>
<add-message-property key="resourceType" value="HTTP Request"/>
<add-message-property key="resourceName" value="Payload"/>
</message-properties-transformer>
<logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/>
</catch-exception-strategy>
</choice-exception-strategy>
Will only log the message but HTTP status always showing 200 OK.
My basic requirement is to send status code based on the error. Say for example if request is not correct then Bad request with code 400.