Error Handling in Mule 4

Error Handling in Mule 4

Error Handling in Mule 4

What is Error Handling and why do we need it?

Error Handling is the mechanism wherein the errors are controlled, and some flow is executed when Error occurs in the Mule Flow. If there is no error handling written at any of the levels, the Mule Default Error Handler is used, which stops the Flow and logs the console’s exception. If there is an error in the Flow, then the subsequent Flow will not get executed, and the Error is thrown. Error Handling adds a reform in the overall Mulesoft Architecture, making it seamless. MuleSoft Error handling best practices are explained in this Mule 4 Error Handling Blog.

Two types of Errors:

System Errors:  System errors are errors that occur outside of Mule flows.

Messaging Errors: Messaging errors that happen within a flow when there is some issue within the Mule Flow.

 

There are three types of Error Handling Block in MuleSoft:

  • On Error Propagate: On Error Propagate catches the Error and process the Mule Flow inside the On Error Propagate block and return the error response (Status Code: 500).
  • On Error Continue: On Error Continue catches the Error and processes the Mule Flow inside the On Error Continue and returns the success response (Status Code: 200).
  • Try-Catch Scope: Try-Catch scope is used when we want to handle the errors in the inner components. A try-catch scope can be beneficial when we want to add a separate error processing strategy for various components in the Flow.

 

Real-Life Scenario: 

The Concept of Error Handling is beneficial while Developing a MuleSoft Application. Suppose we have made a Mule Flow and while executing that Flow, there is an error. If we want some flow to get performed after the Error occurred, then Error Handling’s concept came into Picture. if we are not using Error Handler then By Default that Error is logged to the console and the Flow stops execution.

 

What would a Reader achieve after reading this blog?

After going through this blog, the reader will understand the Error Handler in MuleSoft and get to know about the uses/benefits of adding Error Handler in their MuleSoft Application.

Let’s Do an Exercise (Implementation):

Step 1: Make Sure you have installed the latest version of MuleSoft Anypoint studio on your system.

Step 2: Create a new project in your MuleSoft Anypoint Studio by clicking on File>New>Mule Project.

Step 3: Name Your Project as Error_Handling_Implementation

Step 4: Add the file module by clicking on Add module in the mule pallet as Add Modules>Validation

Step 5: The Error Handling Components will be present by default under Core in Mule Palette.

Step 6: Let’s Create a Simple Flow.

  • We will create a Listener which will listen at port 8081 (URL: http://localhost:8081/error).
  • The next step is to add “Is not Null Validator“, which will only let the parameter pass, “is not null” and if the passed parameter is null, it will throw an error.
  • After the Validator is completely configured, we will add a Logger which will Log a message “The Flow is Completed”.
  • In The Validator referred to as “Is not Null”, we will configure the value to queryParams.name.
  • To throw an error, we will not pass any queryParams to the URL, so that “Is not null” Validator throws an error.

Step 7:  Flow for On Error Continue:

Let’s See how we will Use On Error Continue

Inside the On Error Propagate Block, we have used a simple logger which will log simple message, i.e. “On Error Propagate Block”. After the “Is not Null” Validator will throw an Error, the subsequent Logger will not be executed. It will go to On Error Propagate Block and log the message written in the Logger to console and return the Error (“Value is Null”) response (Status Code: 500).

 

Let’s See Output on Console after executing the following Flow:

Step 8: Try- Catch Block:

The Try-Catch Block is by default present in the Core Module of Mule Palette.

Flow for Try-Catch Block:

The Listener will listen on port 8081 (URL: http://localhost:8081/error) and process the request to the Try-Catch block. Since we are not passing any queryParams, So “Is not Null” Validator will throw an Error and the Error will go to On Error Continue Block and log the message. This is Try-Catch Block” and return Success status code, and the subsequent Logger will get executed and log the message as “Flow is completed”.

Output of the Following Flow: 

Wrapping Up:

Error Handling allows you to handle different errors and execute the Flow flawlessly after an Error has occurred in the Mule Flow. We can also add Try-Catch Block to handle the errors in the Inner Components. Any Mulesoft developer can use Error handling to construct Mulesoft API for clean and flawless performance. Error handling can facilitate seamless API integration.

 

Error Handling in Mule 4

Leave a Reply

Your email address will not be published. Required fields are marked *