Mule 3 Mulesoft Basics Mulesoft Tutorial

Validation Framework – Handling Business Errors MuleSoft

MuleSoft Validation Framework – Handling Business Errors


In this tutorial of mulesoft validation we will create an exception handling framework that will generate business/logical error and do custom validations to request/response message while mapping mulesoft code and learn how to handle those error.
For example: The message that mulesoft application received should have some validations while mapping to the backend application request, in case of validation failure the application should throw an error with error message.

The validations are:
1. if a is (a < b or a < 10) then generate error with error message “A should not be less than 10 or b”.
2. all the values a or b or c or d sum should be less than 500 else generate error with message “a+b+c+d should be less than 500.”

The above example, can be resolved in couple of ways and we will see one of the most simplest and easy way by creating validation framework.
We will resolve by using dataweave and a custom exception class.

1. First we will create a flow and with basic mapping to dataweave.


Flow structure


2. Now, we will create 2 Java classes, which can be invoked through dataweave to generate exception in “src/main/java”.


ExceptionGenerator.java – this class will call custom exception class and can be modified to perform complex logic

package com.tutorialsAtoZ.exception;

public class ExceptionGenerator {
public static String generateValidationException(String message)
throws ValidationException {
if (!message.contentEquals("")) {
throw new ValidationException(message);
}
return "";
}

}

ValidationException.java – this call extends java exception class and simply generates exception message.

package com.tutorialsAtoZ.exception;

public class ValidationException extends Exception {

private static final long serialVersionUID = 1L;

ValidationException(String message) {
super(message);
}

}
3. To call the java classes we need to write global function in mule xml file.


<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def generateValidationException(message) {
import com.tutorialsAtoZ.exception.ExceptionGenerator;
return ExceptionGenerator.generateValidationException(message);
}
</global-functions>
</expression-language>
</configuration>
4. The global function we created will be called from dataweave to generate error while doing validation


We will modify our dataweave mapping and write the business logic for business validation.

5. On running the mulesoft application we see that desired error is generated but not handled. So Last step is to create the exception handling for the custom exception generated.


The error thrown is of class “com.tutorialsAtoZ.exception.ValidationException” that we have created, and can be catch in choice exception strategy.

<choice-exception-strategy name="Choice_Exception_Strategy">
<catch-exception-strategy when="#[exception.causeMatches('*ValidationException*')]" doc:name="Validation Exception Strategy">
<set-variable variableName="exceptionMessage" value="#[groovy:message.getExceptionPayload().getRootException().getMessage()]" doc:name="Set exceptionMessage"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0 %output application/json --- { errorMessage: flowVars.exceptionMessage }]]></dw:set-payload>
</dw:transform-message>
</catch-exception-strategy>
</choice-exception-strategy>
6. On running the error scenario we can see the a proper error message is generate by mulesoft


Download Mule Project for this tutorial
Varun Goel

About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.

7 comments

Leave a Reply

Your email address will not be published.