About Error Handling
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 whenever a problem occurs within a flow. This error is known as a Mule Error. These errors are handled by the Error Handler component. The Error Handler can enclose On-Error Scopes that you can implement to handle specific errors in your flow. By default, unhandled errors will be 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 can incorporate various On Error scopes designed to match error types and expressions. Within each On Error scope that you configure, you can define an error path that incorporates any number of event processors to handle the error as precisely as you want. The error handler will route errors to the first On Error scope that matches it.
The diagram below illustrates 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.
In the example below, a default payload is set for all unhandled errors:
1 2 3 4 5 6 7 <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.
In the example below, a flow is set up with an error handler that logs all errors through a reference.
1 2 3 4 5 6 7 8 9 10 <error-handler name="loggingErrorHandler"> <on-error-continue> <logger message="#['Error: ' ++ error.description ++ ', Payload: ' ++ payload]"/> </on-error-continue> </error-handler> <flow name="withSharedHandler"> <http:request url="http://example.com"/> <error-handler ref="loggingErrorHandler"/> </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.