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?
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>
Related
I want to write a Data Weave Code wherein if the data is null then it should route to 400. How do I write this in Mule Soft?
My flow is as follows:
HTTP -->Transfomer-->Logger
Tranformer DW code
{
event_ops_type: payload.EDM_generic_consumer_message.event_meta_data.event_operation_type
}
Now what I want to implement is if "event_ops_type" is null then route to 400(Exception Handling)?
You may want to try using a validation module. MuleSoft documentation here.
<validation:is-not-null message="event_ops_type is null!" value="#[flowVars.event_ops_type]" exceptionClass="com.example.MyException" doc:name="Validation"/>
You can also use a Groovy script in a choice block to throw any exception you'd like. Here, it will actually throw a 404 with the API generated exception handling. You could switch this to any exception you'd like.
<choice doc:name="Choice">
<when expression="#[flowVars.event_ops_type != null]">
<logger message="#[flowVars.event_ops_type]" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[throw new org.mule.module.apikit.exception.NotFoundException(flowVars['event_ops_type'] + " is null!"); ]]></scripting:script>
</scripting:component>
</otherwise>
</choice>
<exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
You can use choice router after transformer to check payload.event_ops_type == "400". Then either raise custom exception for exception handling or set response status and reason based on event_ops_type 400.
How to find Http error payload in exception object ?
System 1 -> Call's Mule server . Mule server -> Docusign / some one else.
Docusign / some one returns 400 error with json payload (includes errorCode etc)
I need to return the exact details back to System 1.
how will i do this?
1) I did try to add all codes in success codes 200..599, then even error is having 200 ok . This will cause more problem, because I have to add lot of choice routing as some flows are having more data conversion.
2) Ideal soln -> Raise exception as usual. This has to be caught in choice exception. Now I have to send the same status_code and the json response back to the caller.
Issue -? how can i find the payload returned by HTTP in exception object. Where is stored in exception object.
Here is the sample- one way of handling this kind of scenario , use validation Component to throw exception and capture in your mainflow either by exceptionHandling or by APIKIT ExceptionHandling
<flow name="routeexceptionFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger"/>
<http:request config-ref="HTTP_Request_Configuration" path="/in" method="POST" doc:name="HTTP">
<http:success-status-code-validator values="200..599"/>
</http:request>
<validation:is-false config-ref="Validation_Configuration" message="#[payload]" exceptionClass="nz.co.exception.BadRequestException" expression="#[message.inboundProperties['http.status'] ==400]" doc:name="Validate Bad Request"/>
<validation:is-false config-ref="Validation_Configuration" message="#[payload]" expression="#[message.inboundProperties['http.status'] !=200]" doc:name="Capture all other Exceptions"/>
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy when="#[exception.causeMatches('nz.co.exception.BadRequestException')]" doc:name="Catch Exception Strategy">
<set-payload value="#[exception.message]" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
</catch-exception-strategy>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<logger message="****log all other exception****" level="INFO" doc:name="Logger"/>
</catch-exception-strategy>
</choice-exception-strategy>
</flow>
Add this class in your java folder
package nz.co.exception;
public class BadRequestException extends Exception {
private static final long serialVersionUID = 8546839078847602529L;
public BadRequestException() {
super();
}
public BadRequestException(String message) {
super(message);
}
public BadRequestException(Throwable t) {
super(t);
}
public BadRequestException(String message, Throwable t) {
super(message, t);
}
}
In your project if you are using APIKIT Router , then instead of using exceptionHandling as above, add directly nz.co.exception.BadRequestException in 400 ReturnCode apikitExceptionHandling.
asking this for some mulesoft expertise.
the following exception mapping strategy is supposed to branch on hhtp.status 401, 403, 429 but keeps on falling into the 401 branch for status codes 401 and 403 (at least, and determined by both debugging and log written to console):
<apikit:mapping-exception-strategy doc:name="waysact-adaptor-main-exception-strategy">
<apikit:mapping statusCode="401">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 401 = #[payload]" level="INFO" doc:name="log-http-401"/>
</apikit:mapping>
<apikit:mapping statusCode="403">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 403 = #[payload]" level="INFO" doc:name="log-http-403"/>
</apikit:mapping>
<apikit:mapping statusCode="429">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 429 = #[payload]" level="INFO" doc:name="log-http-429"/>
</apikit:mapping>
<apikit:mapping statusCode="400">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging anything = #[payload]" level="INFO" doc:name="logging-anything"/>
</apikit:mapping>
</apikit:mapping-exception-strategy>
is this because it is branching only on exception type org.mule.module.http.internal.request.ResponseValidatorException? i thought it was meant to branch on the status code?
there is another strategy, choice-exception-strategy, that should branch on different exception object types.
Branching of exception based on http.status can be defined with choice exception strategy as below example,
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy when="#[message.inboundProperties.'http.status'=='404']" doc:name="Catch Exception Strategy" >
<logger message="Exceptions message is ... #[exception.message]" level="ERROR" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
<catch-exception-strategy doc:name="Catch Exception Strategy" >
<logger message="Exception message is ... #[exception.message]" level="ERROR" category="com.project.stacktrace" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
</choice-exception-strategy>
APIkit matches the exception based on the exception value defined and not by the statusCode defined. Use choice exception strategy and define multiple catch exception strategy in it. Make sure each catch exception strategy matches a unique exception to make the way for proper http.status code and desired exception payload.
For example, if you want to have 400 to be thrown, make sure a catch exception strategy matches BadrequestException. If the status codes are coming out of the HTTP requester, match the ResponseValidatorException and set the incoming http.status and exception payload
In Mule, am trying to validate a XML against XSD.
In my flow am using schema-validation-filter component nested with in message-filter component and working as expected.
but my question is, if XML fails, how do I capture the error details in logger component and based on results in need to navigate the flow.
Basically am looking for a MEL to print the error description on the console
Note: "returnResult" is set to false, because i need, to do further operations on the XML.
here is my configuration Flow:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<mulexml:schema-validation-filter schemaLocations="D:\MuleStudio\workspace\exceptiontest\src\test\resources\sample.xsd" returnResult="false" name="Schema_Validation" doc:name="Schema Validation"/>
<flow name="ExceptionTestFlow2" doc:name="ExceptionTestFlow2">
<http:inbound-endpoint exchange-pattern="one-way" host="localhost" port="8081" path="dataQ" doc:name="HTTP"/>
<expression-filter expression="#[message.payload != '/favicon.ico' ]" doc:name="Expression does't allow /favicon.ico"/>
<set-payload value="<data>
<books>
<book edition='1.0'>
<auth> auth</auth>
<title>title</title>
<pulbisher>pub</pulbisher>
</book>
</books>
</data>
" doc:name="XML is set as payload tp the current flow"/>
<logger message="::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Actual Flow Started :::::::::::::::::::::::::::::::::::::::::::::::::" level="INFO" doc:name="indicates Actual Flow Started Logger"/>
<logger message="paylad is :::: #[payload] :::::" level="INFO" doc:name="Prints current Payload"/>
<vm:outbound-endpoint exchange-pattern="one-way" path="dataQ" doc:name="VMQ_stores current xml which needs to be validated_senderQ"/>
</flow>
<flow name="ExceptionTestFlow1" doc:name="ExceptionTestFlow1">
<vm:inbound-endpoint exchange-pattern="one-way" path="dataQ" doc:name="VMQ_stores current xml which needs to be validated_receiverQ"/>
<logger message="::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Flow Started :::::::::::::::::::::::::::::::::::::::::::::::::" level="DEBUG" doc:name="indicates Flow Started Logger"/>
<message-filter doc:name="filter to validate xml against xsd" throwOnUnaccepted="true">
<filter ref="Schema_Validation"/>
</message-filter>
<logger message=" validation is true and the payload is :::: #[payload]" level="INFO" doc:name="current Payload"/>
<logger message="::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End of Actual Flow:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" level="INFO" doc:name="indicates Flow End Logger"/>
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy when="exception.causedBy(org.mule.api.routing.filter.FilterUnacceptedException)" enableNotifications="false" doc:name="FilterUnacceptedException">
<logger message="::::::::::::::::::::::::::::::::::: in FilterUnacceptedException :::::::::::::::::::::::::" level="ERROR" doc:name="indicates FilterUnacceptedException Flow Started Logger"/>
<set-payload value="The request cannot be processed, the error is #[exception.getMessage()]" doc:name="Set Payload"/> <!-- [1] -->
<set-property propertyName="http.status" value="500" doc:name="Property"/>
<logger message=":::::::::::: end of FilterUnacceptedException with a payload as #[message.payload] ::::::::::::" level="INFO" doc:name="indicates FilterUnacceptedException End Logger"/> <!-- [2] -->
</catch-exception-strategy>
<catch-exception-strategy enableNotifications="false" doc:name="default">
<logger message=":::::::::::::::::::::: in default::::::::::::::::::::::::::::" level="ERROR" doc:name="indicates defaultException Flow Started Logger"/>
<set-payload value="The request cannot be processed, the error is #[exception.getSummaryMessage()]" doc:name="Set Payload"/>
<logger message="::::::::::::::::::::::::::::::::::::::::::::: END of Default Exception :::::::::::::::::::::::::::::::::::" level="INFO" doc:name="indicates DefaultException End Logger"/> <!-- [1] -->
<!-- [2] -->
</catch-exception-strategy>
</choice-exception-strategy>
</flow>
<flow name="Flow_If_XML_not_valid" doc:name="Flow_If_XML_not_valid">
<logger message=":::::::::::::::::::::::::::: #[payload] :::::::::::::: onThrow ::::::" level="INFO" doc:name="Should Print error description"/>
</flow>
</mule>
Have added an exception strategy that gives the following output:
Message has been rejected by filter. Message payload is of type: String
Code : MULE_ERROR--2
Exception stack is:
1. Message has been rejected by filter. Message payload is of type: String (org.mule.api.routing.filter.FilterUnacceptedException)
... http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/routing/filter/FilterUnacceptedException.html)..
I need to be able to pinpoint which field is causing the problem.
Since the filter is configured to throw on unaccepted, you need to have an exception strategy in place in order to deal with the error.
Read: http://www.mulesoft.org/documentation/display/current/Error+Handling
You don't, see the SchemaValidationFilter source file. The SAXException is just catch and log (in debug). I think you need make your own SchemaValidationFilter. (Inherit Mule Validator)
package what.the.fuck.customFilter;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import org.mule.api.MuleMessage;
import org.mule.api.transport.PropertyScope;
import org.mule.module.xml.filters.SchemaValidationFilter;
import org.xml.sax.SAXException;
public class SchemaValidationFilterWithTrace extends SchemaValidationFilter {
public final static String SAX_ERROR_PROPERTIES = "SAX_ERROR_PROPERTIES";
public final static boolean RETURN_RESULT = false;
public final static String SCHEMA_LOCATION = "what.the.fuck.xsd";
public SchemaValidationFilterWithTrace(){
super();
super.setReturnResult(false);
super.setSchemaLocations(SCHEMA_LOCATION);
}
/**
* Accepts the message if schema validation passes.
*
* #param message The message.
* #return Whether the message passes schema validation.
*/
#Override
public boolean accept(MuleMessage message)
{
Source source;
try
{
source = loadSource(message);
}
catch (Exception e)
{
if (e instanceof RuntimeException)
{
throw (RuntimeException) e;
}
if (logger.isInfoEnabled())
{
logger.info("SchemaValidationFilter rejected a message because there was a problem interpreting the payload as XML.", e);
}
return false;
}
if (source == null)
{
if (logger.isInfoEnabled())
{
logger.info("SchemaValidationFilter rejected a message because the XML source was null.");
}
return false;
}
DOMResult result = null;
try
{
if (super.isReturnResult())
{
result = new DOMResult();
createValidator().validate(source, result);
}
else
{
createValidator().validate(source);
}
}
catch (SAXException e)
{
message.setProperty(SAX_ERROR_PROPERTIES, e.getMessage(), PropertyScope.OUTBOUND);
if (logger.isDebugEnabled())
{
logger.debug(
"SchemaValidationFilter rejected a message because it apparently failed to validate against the schema.",
e);
}
return false;
}
catch (IOException e)
{
if (logger.isInfoEnabled())
{
logger.info(
"SchemaValidationFilter rejected a message because there was a problem reading the XML.",
e);
}
return false;
}
finally
{
if (result != null && result.getNode() != null)
{
message.setPayload(result.getNode());
}
}
if (logger.isDebugEnabled())
{
logger.debug("SchemaValidationFilter accepted the message.");
}
return true;
}
}
I define a flow. It publishes a Web Service with CXF, exchange-pattern: request-response. The Service defines/throws exception type: CAEService_Exception. The web service implementation is almost a dummy. The ws request is processed by other message processors in the flow. During the flow, other components or the ESB itself could throw different exceptions, e.g. ConnectionException, ApplicatOneException, or CompnentTwoException what forever.
In such case, it is important to throw only (business exception) the CAEService_Exception to the client!
How to get(catch) the different exceptions, and how to process/handle them so that the CXF module throws CAEService_Exception to client?
Thanks for your advice!
Regards
Dianlong
<flow name="CAEService" doc:name="CAEService">
<http:inbound-endpoint address="${http.host.inbound}/ebiz/ws/CAEService" exchange-pattern="request-response" doc:name="HTTP">
<cxf:simple-service serviceClass="org.mule.j2a.ws.cae.CAEService"/>
</http:inbound-endpoint>
<component class="org.mule.j2a.ws.cae.CAEServiceImpl" doc:name="Java"/>
<flow-ref name="Fest_ERPs" doc:name="Sub Flow with transformers, call other services, etc."/>
<!-- exceptions -->
</flow>
You can use a choice-exception-strategy to catch the different ones.
eg
<flow name="Sample_Flow">
...
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy doc:name="Catch Exception Strategy"/>
<rollback-exception-strategy doc:name="Rollback Exception Strategy"/>
</choice-exception-strategy>
</flow>
and then throw something else to the client in there respecive implementatinos
<catch-exception-strategy name="Catch_Exception_Strategy">
-- do you stuff
</catch-exception-strategy>