AKB
AKB

Reputation: 5938

Mule Rest exception handling

Mule Flows:

<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 component:

Rest Component:

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();

When I run this, it goes to catch block in Java Rest component with error status as 500. But in Mule flows i am expecting flow should reach

'catch-exception-strategy doc:name="Audit Exception" >

block, but it doesn't reach there, instead it reaches to line 10. How do I handle this?

Upvotes: 4

Views: 698

Answers (1)

AKB
AKB

Reputation: 5938

I made rest component to throw checked custom exception instead of returning Rest Response status:

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

And in my exception flows, made it as:

<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>

Upvotes: 2

Related Questions