Mule Rest异常处理

骡流:

<jersey:resources doc:name="REST">
<component class="com.test.qb.rest.MapIIFContent"/>
<jersey:exception-mapper class="com.test.qb.exception.IIFExceptionMapper" />
</jersey:resources>
<catch-exception-strategy doc:name="Audit Exception" >      
    <set-variable variableName="status" value="Failure" doc:name="Status"/>
    <flow-ref name="QB:audit" doc:name="audit"/>
    <http:response-builder status="500" contentType="application/json" doc:name="Status Code"/>
</catch-exception-strategy>
<logger message=" ===Reached here====" level="INFO" doc:name="Logger"/> <!-- Line 10-->

Java Rest组件:

休息组件:

try{
    String s =null;
    s.toString();// throw nullpointer exception
} catch (IIFException e) {
    return Response.status(500).entity(e.getMessage()).type("Application/json").build();
}
return Response.ok(res).build();

当我运行它时,它将在Java Rest组件中捕获块,错误状态为500.
但在骡子流中,我预计流量应达到

‘catch-exception-strategy doc:name=”Audit Exception” >

阻止,但它没有到达那里,而是到达第10行.我该如何处理?

最佳答案 我使rest组件抛出已检查的自定义异常而不是返回Rest Response状态:

try{
    String s =null;
    s.toString();
} catch (IIFException e) {
    throw new IIFException(e.toString(),e.getCause());
}
return Response.ok(res).build();

在我的异常流程中,将其设为:

<catch-exception-strategy doc:name="Audit Exception" >
    <expression-component doc:name="Create error response"><![CDATA[#[payload = "{\"status\":\"error\", \"message\":\"" + exception.cause.message + "\"}"]]]></expression-component>
    <http:response-builder status="500" contentType="application/json" doc:name="Status Code"/>
</catch-exception-strategy>
点赞