3
votes

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.

1
Show your config with <catch-exception-strategy> and what error you're getting? - Charu Khurana
Yes. It works with Rollback exception strategy only. Don't know why, but it doesn't work with Catch exception strategy. - Jeet

1 Answers

5
votes

Define two outbound properties: http.status and http.reason. These will be sent to the client as the actual status. Without setting these values Mule will default to 200 - OK.

I do not see these fields defined in your example.

Example from the documentation link you provided:

<set-property propertyName="http.status" value="500" doc:name="Property"/>
<set-property propertyName="http.reason" value="Request successfully executed!" doc:name="Property"/>

Make sure you scope these as outbound properties.

Keep in mind that on certain versions of the run time there is a bug where http.reason is not mapped into the response correctly resulting in something like Status 400 - OK rather than Status: 400 - Bad Request. https://www.mulesoft.org/jira/browse/MULE-9045