Creating an Exception Inside Dataweave

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.

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

  1. Use a Lookup function to call a flow containing a groovy component that throws an error with desired message.

               thrownewException(‘Exception 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

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

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

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.