Search code examples
mulemule-componentanypoint-studio

How to retrieve statusCode from ApiKit mapping in Mule


We want to process all of the exceptions we catch with the same process in our exception strategy. It is the one generated by apikit. We first send a lot with a custom component and then we generate json response. In the response with DataWeave we want to set the same statusCode. However, the statusCode do not seem to be a variable that is retrievable. Am I correct, or there is a good way to retrieve it?

    <apikit:mapping-exception-strategy xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" name="svc0031_hotel-apiKitGlobalExceptionMapping">
         <apikit:mapping statusCode="504">
            <apikit:exception value="org.mule.api.transformer.TransformerMessagingException"/>
            <flow-ref name="svc0031_manageErrors" doc:name="Manage Error"/>
        </apikit:mapping>
		<apikit:mapping statusCode="404">
            <apikit:exception value="org.mule.api.transformer.TransformerMessagingException"/>
            <flow-ref name="svc0031_manageErrors" doc:name="Manage Error"/>
        </apikit:mapping>
    </apikit:mapping-exception-strategy>
    <sub-flow name="svc0031_manageErrors">
        <set-payload value="#[groovy:message.exceptionPayload.rootException.message]" doc:name="Set BIP Payload"/>
        <custom-transformer class="se.zystems.baseline.BaselineLogging" doc:name="Log BIP Outgoing">
            <spring:property name="Level" value="ERROR"/>
            <spring:property name="ObjectId" value="CatchErrors"/>
            <spring:property name="TransactionStatus" value="failed"/>
        </custom-transformer>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{ 
  status : 400,
  message : payload,
  code : 42,
  more_info :"https://anypoint.mulesoft.com/apiplatform/nordic-choice-hotels"
}]]></dw:set-payload>
        </dw:transform-message>
        <logger level="INFO" doc:name="Logger"/>
    </sub-flow>


Solution

  • I found out where the statusCode value is stored thanks to the answer by Jesús Pablo Fernández

    He was almost right in his answer, however, the statusCode is stored not in message.inboundProperties['http.status'], but in message.outboundProperties['http.status']. No need to even extract a variable, one can just access it in data transformer directly like this:

            <dw:transform-message doc:name="Transform Message">
                <dw:set-payload><![CDATA[%dw 1.0
    %output application/json
    ---
    { 
      status : outboundProperties['http.status'],
      message : payload,
      code : 42,
      more_info :"https://anypoint.mulesoft.com/apiplatform/nordic-choice-hotels"
    }]]></dw:set-payload>
            </dw:transform-message>