从Mule中的端点捕获错误Java响应 [英] Catch error java response from endpoint in Mule
问题描述
我有一个引擎设置,可以在给定的端点上验证xml
.
localhost/rules/my-rule
,如果xml
文件一致,则返回xml
内容.如果不是,则会引发异常:
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:组合不匹配
GeneralException: Combination missmatch
这是我声明的例外.这是正常的Java异常.
当我使用邮递员之类的工具发布错误的文件时,会得到以下结果:
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
那将是完美的.但是,与m子一起发送时,我得到:
Which would be perfect. However upon sending it with mule I get:
路由0:在异常策略中捕获异常:资源'
http://localhost/rules /my-rule/executions '失败:请求错误(400).
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 ' http://localhost/rules/my-rule/executions ' 失败:错误的请求(400). detailDescription =资源上的HTTP POST ' http://localhost/rules/my-rule/executions ' 失败:错误的请求(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 =文本/纯文本; charset = UTF-8
attribute = org.mule.extension.http.api.HttpResponseAttributes {
状态码= 400原因短语=标题= [ 内容类型=文本/纯文本 transfer-encoding =分块 日期=星期三,2019年7月3日07:16:19 GMT connection = close]} propertiesMediaType = /} childErrors = []}]]}
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=[] }] }
我的流程配置:
<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>
推荐答案
使用全局错误处理程序,我已经为您编写了HTTP错误请求.您可以将其扩展到其他错误类型,例如API工具包等.
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>
这篇关于从Mule中的端点捕获错误Java响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!