Author: Rithul Kumar
This document briefs on how to raise error inside dataweave based on conditions provided. DW 2.0 provides two operators “fail” and “failif” to create errors inside dataweave code.
How it worked in Mule 3:
Assume a scenario where you need to throw an error for a particular value in the incoming field. There were 2 methods that is mostly used.
- Use a Validation component to validate the field value.
Limitation : If the validation is checking only the datatype, then particular field value validation is not possible
- Use a Lookup function to call a flow containing a groovy component that throws an error with desired message.
How it works in Mule 4
Mule 4 has a Runtime[dw::Runtime] module which allows the user to interact with dataweave engine. This module provides two operators to create error inside dataweave
- Fail operator
This throws the error message when specified. Usually its provided based on a condition
This sample code will throw error with the given message
- Failif operator
This operator throws an error when the given expression is true, else it forwards the given value.
This code will throw the given error
Note: Make sure to import the Runtime module before using these two operators