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-maven–plugin</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
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://staging.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:
However, it does not find any flow from my reusable plugin. That’s the reason, I had to create shared flow.