Exception Strategy in Mulesoft


In this walk through, we will enhance our message flows to handle exceptions in better way. Here is a simple mule flow, which receives a booking request by HTTP post method and then it does HTTP oubound POST calls to a restapi to book the service requested. Once the response is received, its is archived in DB for reference.
Lets add the exception handling block to both the message flows. Drag and drop a exception strategy to the main flow and then bring in set payload and property components as shown below.
 Similarly, add exception strategy to hotel booking flow as shown below.
Lets run the project and test the mule application. You could see the flow succeed but the response got is a java object, slightly modify the flow to render the booking response.
Lets capture the booking response in a flow variable and then use a set payload to update the payload with flow variable data and render it back to the client as shown.
Now lets test the application with valid booking request and booking response is received in the client.
Lets try to post a invalid booking request(ContactNumber element is posted as ContactNumber1), this would cause a metadata mismatch and an exception would be thrown, we received as 406 response from the hotel booking flow.
In offline, I renamed the database name. Now lets post a valid request and see the behavior as expected exception related to database is thrown.
Instead of having exception strategy for each individual flow, all the exception strategy can be consolidate using choice exception strategy. This allows us to define conditions at each exception strategy and have a default strategy when there is no match. Now, remove the exception strategy from main flow and move it into the choice exception strategy as shown.
Bring in a reference strategy to the error handling block of main flow and configure the choice exception strategy as shown.
Lets replace the HTTP outbound interaction with flow reference activity, this would help us to merge a common exception handling for complete mule flow and we would bring in the exception strategy from hotel flow into the choice exception strategy as shown.
Now lets run and test the mule application. 
Invalid ContactNumber element in the payload.
Database offline
Lets change the default exception handling for the application. Create a global configuration element in the global.xml file. Delete the reference exception strategy in mule application and move the existing exception handling strategies from mule application to global.xml.

Switch to the Global Elements view. Click the Create button. In the Choose Global Type dialog box, select Global Configurations > Configuration and click OK. 
Lets re-test the mule application and verify the behavior. Offline I changed the archive table name and default exception block is executed.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.