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. The validations are: 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. |
1. First we will create a flow and with basic mapping to dataweave.
|
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 |
Validation Framework – Handling Business Errors MuleSoft

Nice example
Thank you