Mule 4 — Error Handling

Irushinie Muthunayake
Geek Culture
Published in
7 min readDec 9, 2021

--

PART — 1

✦ Handling errors are much important when dealing with developing APIs.

✦ An error can be defined as an unexpected event that happens while processing a computer program. To respond to those exceptions, Exception Handling was introduced.

Let's move to Error Handling in Mule 4

In Mule4, we can use On Error Continue, On Error Propagate, and Raise error at the flow level.

https://www.ricston.com/blog/mule-4-error-handling-review/

Custom Global error handler and Default error handler can be used at the project level.

At the time when an error occurred, an error object is created. It has some properties like error type, error description..etc.

✲ Error Type ➔ is a mixture of Namespace and Identifier.

Mule 4 identifies the error based upon the error type and then directs to the respective error handling block.

Important ✍️✍️✍️✍️

The error will direct to error handling only if it identifies that the specific error type of that error is handled.

Example: If you are getting an HTTP: Not found error but you are handling for Http: unauthorized error, then it will not route for your error handling block as it doesn't match the error type.

How does error handling work in mule4?

Let's follow the below steps for ease of understanding.

☆ First, see anything present in the error handling block.

☆ If yes, see whether the particular(on error propagate or on error continue) error type is matched.

☆ If not, the mule will use its Default Error handling.

☆ And if the flow is not called by any other flows, then it will show the default value that is set in the Error response and will give the status code as 500. (If nothing is set manually) But if your flow is called by another flow, then it will raise as an error to the particular calling flow.

☆ If something is present in Error Handling and if the particular error type is matched,

Let's understand these steps using the below examples….

Example 1 :

So here, As there is nothing present in the error handling block, it will go to the default error handler and give a 500 status code with the error description as below.

Example 2 :

Accordingly, As an On error propagate is presented in the error handling section and the error type is also matched, it will go to the On error propagate and will give a 500 status code with the latest payload which is presented in the error handling block.

Example 3 :

According to example 3, As an On error continue is presented in the error handling section and the error type is also matched, it will give the 200 success status code with the latest payload which is presented in the error handling block.

Output for Example 3

Example 4 :

According to example 4, An On error continue and an On error propagate are present in the error handling section, and as the error type in the on error continue is not matched, it will give the payload value of On error propagate block with 500 status code.

Below are some of the practical examples of error handling in mule4.

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

Example :

Output:

--

--