Search
Close this search box.

Global Error Handler Plugin in Mule 4

Mulesoft redesigned the whole error handling mechanism and components in Mule 4 (some key difference here). In Mule 3, there was a simple way to reuse a flow across multiple applications by putting into a separate mule-application and package that as jar. Jar can be added as a dependency in any mule-application.  An important application of this was having a reusable error response builder flow that possibly analyzes the exception and generates client friendly error response with the proper error code and error message for the REST API client.

However, Mule 4 does it a slightly different way. For reusing a flow, we need to create a Mule plugin and put the flow into that plugin. Any mule-application can add that plugin as maven dependency and reusable flow can be shared.

This article gives a step-by-step guide to generate that plugin in Mule 4 and installing into any mule-application.

  • Create a new mule project in Anypoint studio and go to pom.xml. Make sure the packaging set as mule-application. In our case the groupId is “com.plektonlabs” and the artifactId: “com-plektonlabs-utils-errorResponseBuilder”

<packaging>mule-application</packaging>

  • Go to Build tag in pom.xml and make sure that the value extension tag is set as true and configuration->classifier is set as mule-plugin like the example below:

<build>

                    <plugins>

                           <plugin>

                                  <groupId>org.mule.tools.maven</groupId>

                                  <artifactId>mule-mavenplugin</artifactId>

                                  <version>${mule.maven.plugin.version}</version>

                                  <extensions>true</extensions>

                                  <configuration>

                                        <classifier>mule-plugin</classifier>

                                  </configuration>

                           </plugin>

                    </plugins>

       </build>

  • Implement your global error handler flow. In my case, I tried to create a global on-error-propagate handler to be able to reuse in each of my mule-application. The idea is to use this global-on-error-propagate flow in the API main flow in every project

plektonlabs-global-error-handler-mulesoft-flow-main

plektonlabs-global-error-handler-mulesoft-flow-impl

Now, you can run the “mvn clean install” command in your command prompt and refer that as dependencies in your intended mule-application. if you need detail steps then follow my other article here: https://www.plektonlabs.com/installing-global-error-handler-into-a-mule-4-application-as-maven-dependency/

Surprisingly enough, while writing this article I didn’t find any better way to achieve this reusability. I really wanted to have an error handler instead of implementing a flow. There is a component in Mule 4 named “Configuration” in which you can refer your error handler, like the following:

plektonlabs-mulesoft-issue-with-configuration-global-error-handler

However, it does not find any flow from my reusable plugin. That’s the reason, I had to create shared flow.

Share This Post

More To Explore

Productivity with MuleSoft RPA
Blogs

Top 5 reasons to invest in MuleSoft RPA

Having trouble feeding data into your system repetitively? Or having to extract a bunch of emails from months past? Or are you overloaded with complex

How can we help?

A little about yourself and we're ready to go

We pride ourselves on swift communication and prompt responses. Let us know what you're thinking and how we can help you.

Contact Information​

Head Office
18 King Street E, Suite 1400, Toronto ON M5C 1C4, Canada

Durham Office
Unit 265, 1099 Kingston Rd. Pickering, ON. L1V1B5, Canada

Austin Office
5900 Balcones Dr, STE 4000 Austin, TX 78731, USA

Dallas Office
Unit 113, 320 Decker Drive, Irving, Texas, TX 75062, USA

Phone: +1(877) 855-8775
Email: info@plektonlabs.com

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Going into 2023, data will continue to be the most valuable asset for businesses! You need to know how to maximize the value of your data via integration. Learn more here.