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://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

Share on facebook
Share on linkedin
Share on twitter
Share on email

More To Explore

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
75 Cornwall Drive, Ajax ON L1T3G2

Toronto Office
602 – 8133 Warden Ave, Markham, ON L6G 1B3, Canada

Phone: +14168902757
Email: [email protected]

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