Catch error java response from endpoint in Mule

2019-08-12 06:21发布

I have an engine set up that validates an xml at a given endpoint.
localhost/rules/my-rule, if the xml file is consistent it returns the xml content. If not however, an exception is thrown:

GeneralException: Combination missmatch

Which is an exception I declared. It's a normal Java Exception.
When I post a faulty file with a tool like postman I get this result:

Can't invoke perform method on "class %rule class%" with request data of 
"%data sent to endpoint%" 
(reason: ch.package.GeneralException: Combination missmatch). Combination missmatch

Which would be perfect. However upon sending it with mule I get:

Route 0: Caught exception in Exception Strategy: HTTP POST on resource 'http://localhost/rules/my-rule/executions' failed: bad request (400).

Why can't I just get the error message? There isn't more insight when looking at the log:

org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation { description=HTTP POST on resource 'http://localhost/rules/my-rule/executions' failed: bad request (400). detailedDescription=HTTP POST on resource 'http://localhost/rules/my-rule/executions' failed: bad request (400). errorType=HTTP:BAD_REQUEST
cause=org.mule.extension.http.api.request.validator.ResponseValidatorTypedException errorMessage= org.mule.runtime.core.internal.message.DefaultMessageBuilder$MessageImplementation {
payload=org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider@3239ebf7 mediaType=text/plain; charset=UTF-8
attributes=org.mule.extension.http.api.HttpResponseAttributes {
Status Code=400 Reason Phrase= Headers=[ content-type=text/plain transfer-encoding=chunked date=Wed, 03 Jul 2019 07:16:19 GMT connection=close ] } attributesMediaType=/ } childErrors=[] }] }

My flow configutation:

<scatter-gather doc:name="Distribute workload" doc:id="15151949-c61d-4629-9fd4-0a4e16d62eec" >
    <route >
        <!--Access to localhost/rules/my-rule>
        <flow-ref doc:name="CategoryToType" doc:id="e5d39afa-3b2a-45ef-81c0-afce77a76aef" name="validateCategoryToType" />
    </route>
</scatter-gather>
<error-handler>
    <on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="16001c58-7609-42a6-9bfa-1cd756998f27" >
        <logger level="INFO" doc:name="Logger" doc:id="8ec2af18-3e45-4d15-a373-5e9af74723d7" message='#[error]'/>
    </on-error-propagate>
</error-handler>
<sub-flow name="validateCategoryToType" doc:id="da38db3a-7d51-4fd1-bd06-9e58b872d468" >
    <foreach doc:name="For Each" doc:id="ff9944ba-5f87-4a5a-9392-a4b27c2f5fb0" collection="payload.Kunden" rootMessageVariableName="Kunden">
        <ee:transform doc:name="Transform Message" doc:id="f33fa5ea-0b4d-4ac5-a247-90fd6978248d">
            <ee:message>
                <ee:set-payload>
                    <!--Some transformy stuff-->
                </ee:set-payload>
            </ee:message>
        </ee:transform>
        <http:request method="POST" doc:name="Request" doc:id="53df928a-4fd1-4125-88e0-be73b006beee" config-ref="HTTP_Request_configuration" path="/rules/my-rule/execution>
            <http:headers><![CDATA[#[output application/java
                ---
                {
                "Content-Type" : "application/xml",
                "Accept" : "application/xml"
                }]]]></http:headers>
        </http:request>
            <logger level="INFO" doc:name="Logger" doc:id="cc02312e-b71b-461c-bba0-077bfb1e9b7e" message="#[payload]" />
    </foreach>
</sub-flow>

1条回答
该账号已被封号
2楼-- · 2019-08-12 06:42

Use Global error handler, i have composed HTTP error request for you. You can expand it to other error types like API tool kit etc..

Also, extend further error messages by creating variables and capture postman payload into a variable

<on-error-propagate type="HTTP:BAD_REQUEST"
                  enableNotifications="true" logException="true" doc:name="On Error Propagate"
                  doc:id="73b29eaf-ebcd-4857-baea-4c2fb963b055">
                  <set-variable value="#[400]" doc:name="HTTP Status - 400"
                        doc:id="4017a80c-f968-4f09-8fbb-eaa4ce5d1413" variableName="httpStatus" />
                  <set-variable
                        value="Service is unable to handle request"
                        doc:name="errorMessage" doc:id="9ab2b50b-7aad-4460-b276-f5d50ffd1efe"
                        variableName="errorMessage" />
                  <ee:transform doc:name="Transform Message" doc:id="df98f927-584c-454e-971e-79d2ff1e842d" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
error: {
    errorCode: error.errorType.identifier,
    errorDateTime: now() as String { format: "yyyy-MM-dd'T'HH:mm:ss" },
    errorDescription:  error.description
}]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <ee:transform doc:name="Transform Message" doc:id="6e6c0017-2b23-4bfa-83ef-2f77cc25aa08" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>

</on-error-propagate>
查看更多
登录 后发表回答