Basic Internal Error Handlers in Mule application

Introduction

This article is the continued part in the MuleSoft articles series; if you would like to understand the basics of MuleSoft, refer to these articles before going next,

Getting Started

In this article, we will discuss 2 types of error-handling mechanisms in Mule 4.

  1. On-Error-Continue: This feature allows you to instruct MuleSoft to continue processing even when an error occurs. With this approach, you can handle errors within a flow without interrupting the entire integration. It enables you to define alternative routes or fallback mechanisms to handle errors and maintain data integrity.

    Executes and sends the result of the execution to its container Try scope, which uses that result to complete the execution successfully. Any transactions at this point are also committed.

  2. On-Error Propagate: MuleSoft allows you to propagate errors from one component to another within or between different flows. This enables you to centralize error-handling logic and avoid repetitive error-handling code across multiple components.

    Rolls back any transactions, then executes and uses that result to re-throw the existing error, causing its container Try scope's execution to fail.

Let's do a quick project setup and include the required global config files

  • First, open Anypoint Studio and Create a New Mule Project.
  • Enter a project name and selection location and click finish.
  • Click on Add Modules in the Mule palette and find File in the featured components.
  • Drag and drop the File onto the Mule palette before adding it to the project.
  • Now let's add a new Mule Configuration File and give the name as global.
  • Enter the file name as global.
  • Go to Global Elements, click the Create button, and add an HTTP Listener config.
  • Leave everything as default and click ok.
  • Click the Create button, Select File Config, and click OK.
  • Provide the Working Directory path in the General tab.

Next, drag and drop HTTP >> Listener from Mule palette to Message flow and give Display Name, select Connector configuration, and enter the Path.

On-Error-Continue Example

Drag and Drop Listener HTTP from Mule Palette and give a name and select connector configurations and endpoint Path; also, make sure to provide the GET method in advanced settings.

internal error handlers in Mule application

Next, drag and drop the Request (HTTP) component from Mule Palette and provide the name and configuration and request method, and Path.

internal error handlers in Mule application

Next, drag and drop the Logger component and enter the name and message.

  internal error handlers in Mule application

Next, drop and drop the On Error Continue component in the Error handling section from the Core palette and Enter the display name and Type (ANY).

internal error handlers in Mule application

Again, drop and drop Logger inside On Error. Continue to log in and provide the log message.

internal error handlers in Mule application

At last, drag and drop Set Payload and provide the payload data.

internal error handlers in Mule application

internal error handlers in Mule application

On-Error Propagate Example

Let's drag and drop all the components except replace On Error Continue to On Error Propagate. These are all the steps you can follow.

internal error handlers in Mule application

Let's run both examples to see the result in Postman.

internal error handlers in Mule application

Open Postman to see the result and enter the request endpoint to see the result.

internal error handlers in Mule application

As you can see, the endpoint request ran till payload, even after there is an error for connection refusal, and the status is 200 OK. This is the default behavior of on error continue.

internal error handlers in Mule application

For error propagation, there are 500 server error messages, and the request did not run till the payload.

You can also check the application log using the console logs while hitting the endpoint.

internal error handlers in Mule application

Conclusion

In this article, we learned about some basic internal error handlers in Mule application and how to see the result in Postman.


Similar Articles