Mule 4 has redesigned error handling by introducing the error-handler component, which can contain any number of internal handlers and can route an error to the first one matching it. Such handlers are on-error-continue and on-error-propagate, which both support matching through an error type (or group of error types) or through an expression (for advanced use cases). These are quite similar to the Mule 3 choice (choice-exception-strategy), catch (catch-exception-strategy), and rollback (rollback-exception-strategy) exceptions strategies However, they are much simpler and more consistent.
If an error is raised in Mule 4, an error handler is executed and the error is routed to the first matching handler. At this point, the error is available for inspection, so the handlers can execute and act accordingly, relative to the component where they are used (a Flow or Try scope):
-
An on-error-continue executes and uses the result of the execution as the
result of its owner (as though the owner completed the execution successfully).
Any transactions at this point are committed, as well.
-
An on-error-propagate rolls back any transactions, executes, and uses that
result to re-throw the existing error, meaning its owner is considered to be
“failing.”
Consider the following application where an HTTP listener triggers a Flow Reference component to another flow that performs an HTTP request. If everything goes right when a message is received (1 below), the reference is triggered (2), and the request performed (3), which results in a successful response (HTTP status code 200) (4).
If the HTTP request fails with an HTTP:NOT_FOUND error (see 3 below) because of
the error handler configuration in inner-flow, the error is propagated (4), and the
Flow Reference component fails (2). However, because primary-flow handles the
error with on-error-continue, the Logger it contains (5) executes, and a
successful response (HTTP status code 200) is returned (6).
If the request fails with an unauthorized error instead (3), then inner-flow handles it with an on-error-continue by retrieving static content from a file (4). Then the Flow Reference component is successful as well (2), and a successful response (HTTP status code 200) is returned (5).
But what if another error occurred in the HTTP request? Although there are only handlers for NOT_FOUND and UNAUTHORIZED errors in the flow, errors are still propagated by default. This means that if no handler matches the error that is raised, then it is re-thrown. For example, if the request fails with a method not allowed error (3), then it is propagated, causing the Flow Reference component to fail (2), and that propagation results in a failure response (4).
The scenario above can be avoided by making the last handler match ANY, instead of just HTTP:UNAUTHORIZED. Notice how, below, all the possible errors of an HTTP request are suggested:
You can also match errors using an expression. For example, since the Mule Error is available during error handling, we can use it to match all errors with the HTTP namespace: