Error Handling In Mulesoft

(On error continue, On error propagate, global error handler, Try scope-raise error)

Introduction:

What is an error? - An error occurs when an unexpected event happens while processing

What Is Error Handling? -Exception handling is nothing but handling the errors while the unexpected event happens while processing

Levels of error handling

If we are not using any error handler, By default MuleSoft will handle the error and give a proper error message to the user. Mule4 error handling is effortless when compared with Mule3.

At flow level in exception handling using -

  • On error continue
  • On error propagate
  • Raise error

ON ERROR CONTINUE

Scope always returns a successful response to the next level. error in the error handling block it will handle the error and trigger a 200 response to the payload message

ON ERROR PROPAGATE

Always propagates the error to the next level and returns an error response to the next level. error in the error handling block it cannot handle the error and 500 responses will be sent with the error message.

— — Mule4 has the excellent feature of identifying the errors, By connectors we can able to identify the errors. and handle the error by using on-error propagate or on-error continue blocks, to see If that particular error type is handled, If Not Mule will use default error handling.

On Error Continue VS On Error Propagate

Raise Error

Generates a Mule error to indicate that a failure has occurred. You can customize the error’s description and type. We can write our error type and Own error description in Raise Error. It’s not like On-error Continue or On error propagate it has default error cases. We cannot use the default error cases like HTTP, DB, etc…, We should define it.

USECASE

The mainflow contains arithmetic operations like 1+1 which returns the value as 2, and the sub-flow has a similar operation like 1/0 which should throw an error. here we can handle this error by using several error-handling techniques.

Example 1: On Error Continue Error handler

On Error Continue Error handler flow
Main flow — set variable
Child flow — set variable
Child flow — On error Continue
Child flow — On error Propagate

Postman Testing: handles the error and triggers 200 response

Example 2: On Error Propagate Error handler

On Error Propagate, the error handlerflow

Note: Example -2 Flow is the same as the previous one. except,in the main flow we have added error handler as “On error propagate”

Postman Testing : handle the error and triggers 500 response

Global error handler — In MuleSoft, global error handling refers to the process of handling errors and exceptions that occur during the execution of a Mule application at a central location. It provides a way to capture and handle errors consistently across multiple integration flows and components

Example 3: Global Error handler

Global error handler flow

Postman Testing:

Example 4: Raise Error handler

Raise error handler flow
Child flow — set variable
Raise error configuration

Postman Testing:

Thank you

Please provide feedback. It will help to improve the content.

Thank you for reading this article! If you found this response useful, please consider giving it a ‘Clap’ and Follow me for more helpful articles and insights in the future.

@mulesoft @Errorhandling @mule4

--

--