How to use fail function in Mulesoft- Part-2

This blog demonstrates the use of fail function

How to use fail function in Mulesoft- Part-2

 

Step 7:

Populate the path for the api. This gets appended with the host, port while calling the api.

 

Step 8:

Select the Transform component from the component palette. This component is part of core module.

 

Step 9:

Drag & drop the component in mule flow. Double click on Transform component.

 

Step 10: 

Write the dataweave code to leverage fail() which is part of Runtime module.

fail()- This function returns the error with the message which is passed as parameter to the function.

 

Dataweave Code:

%dw 2.0

import * from dw::Runtime

output application/json

---

if(payload.message=="" or payload.message == null) fail("Message is empty") else "Message is not empty"

 

Step 11:  

Deploy the Mule application by right clicking on mule project-> Run As -> Mule Application

 

Step 12:

In the logs we get a success message indicating successful deployment of api.

 

Step 13:

Go to postman and test the api by populating the endpoint.

http://{localhost} : {port}/{path} -> http://localhost:8089/fail

a) Populate the Post body as shown below :
 

{

 

""message" : "Testing the api "

 

}

 

b)  Populate the Post body as shown below :
 

{

 

"message" : ""

 

}

 

c)  Populate the Post body as shown below :
 

{

 

"message" : null

 

}