From a high-level perspective, errors that occur in Mule fall into one of two categories: system errors and messaging errors.
Mule throws a messaging error (a Mule error) whenever a problem occurs within a flow. To handle Mule errors, you can set up On Error components inside the scope-like Error Handler component. By default, any unhandled errors are logged and propagated.
When a Mule Event that is getting processed through a Mule flow raises an error, the normal flow execution stops, and the process is transferred to the Error Handler component. Inside the Error Handler component, you set up On Error components to match error types and expressions. Within each On Error component, the error path you define can incorporate any number of event processors to handle the error as precisely as you want. The Error Handler component will route an error to the first On Error scope that matches the error.
This figure shows what happens when an event processor throws a Mule error:
You can override the default error handling behavior either per flow or within a flow. You can group a sequence of components together into a Try Scope to further override the flow’s error handling behavior. The default Error Handler can also be customized with a reference to the desired handler in the app’s configuration.
This example sets a default payload for all unhandled errors:
<configuration defaultErrorHandler-ref="allErrorHandler"/> <error-handler name="allErrorHandler"> <on-error-continue> <set-payload value="#['An unhandled error occurred: ' ++ error.description]"/> </on-error-continue> </error-handler>
Just as you can reference error handlers to specify the default, error handlers can also be referenced in flows.
This example sets up a flow with an Error Handler that logs all errors through a reference:
<on-error-continue name="loggingErrorHandler"> <logger message="#['Error: ' ++ error.description ++ ', Payload: ' ++ payload]"/> </on-error-continue> <flow name="withSharedHandler"> <http:request url="http://example.com"/> <error-handler > <on-error ref="loggingErrorHandler"/> </error-handler> </flow>
Mule throws a system error when an exception occurs at the system level.
If no Mule Event is involved, the errors are handled by a system error handler.
A system error handler handles exceptions that occur:
During application start-up.
When a connection to an external system fails.
When a system error occurs, Mule sends an error notification to registered listeners, logs the error, and if the error was caused by a connection failure, executes a reconnection strategy.
System error handlers are not configurable in Mule.