Search code examples
mulemulesoftmule4

mule 4 suppress response in case of 500 status error


Using mule 4.4 runtime on premise community edition .

I have a http listener configured as below followed by an API Kit router :

<http:listener-config name="HTTP_Listener_config" doc:name="Listener"  basePath="/abc/lmn" >
    <http:listener-connection host="0.0.0.0" port="8081" readTimeout="5000"/>
</http:listener-config>

<apikit:config outboundHeadersMapName="outboundHeadersMapName" httpStatusVarName="httpStatus" doc:name="Router"  name="Router" api="myApi.yaml" >
        <apikit:flow-mappings >
            <apikit:flow-mapping resource="/students" action="put" content-type="application/json" flow-ref="students" />
        </apikit:flow-mappings>
    </apikit:config>

Now within the flow I am encountering an error while trying to write a file . So the control reaches error handler . Here is my error handler ( on error propogate )

<error-handler >
        <on-error-propagate enableNotifications="false" logException="false" doc:name="pr"  >
        <set-payload value="#[null]" doc:name="Set null Payload"  />
        </on-error-propagate>
</error-handler>

So this is essentially 'throwing' the error . Note that I am explicitly setting the payload as null .

However in postman the response shows error.description not sure why ... ?

Here is the screen print of http listener response tab: enter image description here

Here is the error received in Postman :

500 Server Error

Cannot write to file '/abc/xy.txt' because path to it doesn't exist. Consider setting the 'createParentDirectories' attribute to 'true'

I am not sure why response is getting populated with error details when I have explicitly set payload as null and as per listener configuration in case if error Body is populated with payload ( which is null )

Edit1 : Adding complete code :

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp"
        xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http"
        xmlns="http://www.mulesoft.org/schema/mule/core"
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd">
   
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config"  basePath="/abc/lmn" >
            <http:listener-connection host="0.0.0.0" port="8080" readTimeout="5000"/>
        </http:listener-config>
        <apikit:config outboundHeadersMapName="outboundHeadersMapName" httpStatusVarName="httpStatus" doc:name="Router" name="Router" api="myApi.yaml" >
            <apikit:flow-mappings >
                <apikit:flow-mapping resource="/students" action="put" content-type="application/json" flow-ref="students" />
            </apikit:flow-mappings>
        </apikit:config>
        <sftp:config name="SFTP_Config" doc:name="SFTP Config"  >
            <sftp:connection host="xxx" username="yyy" password="zzz" />
        </sftp:config>
        <flow name="studentFlow"  >
            <http:listener doc:name="Listener"  path="/*" config-ref="HTTP_Listener_config">
                <http:error-response statusCode="#[vars.httpStatus default 500]" reasonPhrase="#[vars.errorReasonPhrase]">
                    <http:body ><![CDATA[#[payload]]]></http:body>
                </http:error-response>
            </http:listener>
            <apikit:router doc:name="APIkit Router"  config-ref="Router"/>
        </flow>
        
        <flow name="students" >
            <until-successful maxRetries="1" doc:name="Until Successful"  millisBetweenRetries="2000">
                <sftp:write doc:name="Write" config-ref="SFTP_Config" path="#['/abc/ab.txt']" createParentDirectories="false" />
            </until-successful>
            
            <error-handler >    
                <on-error-propagate enableNotifications="false" logException="false" doc:name="On Error propogate" >
                
                <set-payload value="#[null]" doc:name="Set null Payload"  />
                
                </on-error-propagate>
            </error-handler>
        </flow>
    </mule>

Solution

  • Not sure if this is a bug or a desired feature but the HTTP Listener defaults to error.description when the payload is null in case of error responses. If you want to send a blank response you can set payload to a blank string '' which will send a blank response.

    I have also noticed that this happens when the mime type of your null payload is application/java (which is default if you do not define it). What I mean is if you would have set your payload to output json --- null you would have gotten null as your HTTP response, so it is always advisable to declare a mime type if you are expecting your payload to be used as an HTTP Response. This is also because it will effect the Conent-Type header of the response.
    For Example, if you set the payload to blank string '' the response will have a Content-Type of application/java which is not an expected one from an HTTP Response. If you set it to output text/plain --- '' it will send the Content-Type as text/plain