Mulesoft 简明教程
MuleSoft - Mule Error Handling
新的 Mule 错误处理是 Mule 4 中进行的最大、最重要的更改之一。新的错误处理可能看起来很复杂,但它更好、更高效。在本章中,我们将讨论 Mule 错误的组成部分、错误类型、Mule 错误类别和用于处理 Mule 错误的组件。
The new Mule error handling is one of the biggest and major changes done in Mule 4. The new error handing may seem complex, but it is better and more efficient. In this chapter, we are going to discuss about components of Mule error, Error types, categories of Mule error and components for handling Mule errors.
Components of Mule Error
Mule 错误是 Mule 异常失败造成的结果,包括以下组件:
Mule error is the result of Mule exception failure has the following components −
Description
它是 Mule 错误的一个重要组成部分,将提供有关问题的信息。其表达式如下:
It is an important component of Mule error which will give description about the problem. Its expression is as follows −
#[error.description]
Type
Mule 错误类型组件用于描述问题。它还允许在错误处理程序内进行路由。其表达式如下:
The Type component of Mule error is used to characterize the problem. It also allows routing within an error handler. Its expression is as follows −
#[error.errorType]
Cause
Mule 错误的 Cause 组件提供了导致错误的底层 Java 可抛出项。其表达式如下:
The Cause component of Mule error gives the underlying java throwable that causes the failure. Its expression is as follows −
#[error.cause]
Message
Mule 错误中的消息组件显示有关错误的可选消息。其表达式如下:
The Message component of Mule error shows an optional message regarding the error. Its expression is as follows −
#[error.errorMessage]
Child Errors
Mule 错误的子错误组件提供了内部错误的可选集合。这些内部错误主要由 Scatter-Gather 等元素用于提供聚合路由错误。其表达式如下:
The Child Errors component of Mule error gives an optional collection of inner errors. These inner errors are mainly used by elements like Scatter-Gather to provide aggregated route errors. Its expression is as follows −
#[error.childErrors]
Example
如果 HTTP 请求的状态代码为 401 失败,则 Mule 错误如下:
In case of failure of HTTP request with a 401 status code, the Mule Errors are as follows −
Description: HTTP GET on resource ‘http://localhost:8181/TestApp’
failed: unauthorized (401)
Type: HTTP:UNAUTHORIZED
Cause: a ResponseValidatorTypedException instance
Error Message: { "message" : "Could not authorize the user." }
Sr.NO |
Error Type and Description |
1 |
TRANSFORMATION This Error Type indicates an error occurred while transforming a value. The transformation is Mule Runtime internal transformation and not the DataWeave transformations. |
2 |
EXPRESSION This kind of Error Type indicates an error occurred while evaluating an expression. |
3 |
VALIDATION This kind of Error Type indicates a validation error occurred. |
4 |
DUPLICATE_MESSAGE A kind of validation error which occurs when a message being processed twice. |
5 |
REDELIVERY_EXHAUSTED This kind of Error Type occurs when maximum attempts to reprocess a message from a source has been exhausted. |
6 |
CONNECTIVITY This Error Type indicates a problem while establishing a connection. |
7 |
ROUTING This Error Type indicates an error occurred while routing a message. |
8 |
SECURITY This Error Type indicates a security error occurred. For example, invalid credentials received. |
9 |
STREAM_MAXIMUM_SIZE_EXCEEDED This Error Type occurs when the maximum size allowed for a stream exhausted. |
10 |
TIMEOUT It indicates the timeout while processing a message. |
11 |
UNKNOWN This Error Type indicates an unexpected error occurred. |
12 |
SOURCE It represents the occurrence of an error in the source of the flow. |
13 |
SOURCE_RESPONSE It represents the occurrence of an error in the source of the flow while processing a successful response. |
在上面的示例中,您可以看到 Mule 错误的消息组件。
In the above example, you can see the message component of mule error.
Error Types
让我们借助 Mule 错误类型的特性来理解错误类型 -
Let us understand the Error Types with the help of its characteristics −
-
The first characteristics of Mule Error Types is that it consists of both, a namespace and an identifier. This allows us to distinguish the types according to their domain. In the above example, the Error Type is HTTP: UNAUTHORIZED.
-
The second and important characteristic is that the Error Type may have a parent type. For example, the Error Type HTTP: UNAUTHORIZED has MULE:CLIENT_SECURITY as the parent which in turn also has a parent named MULE:SECURITY. This characteristic establishes the Error Type as specification of more global item.
Kinds of Error Types
所有错误分类如下 -
Following are the categories under which all the errors fall −
ANY
此类别下的错误是在流程中可能发生的错误。这些错误不太严重,可以轻松处理。
The errors under this category are the errors that may occur in a Flow. They are not so severe and can be handled easily.
CRITICAL
此类别下的错误是无法处理的严重错误。以下是此类别下的错误类型列表 -
The errors under this category are the severe errors that cannot be handled. Following is the list of Error Types under this category −
Sr.NO |
Error Type and Description |
1 |
OVERLOAD This Error Type indicates an error occurred due to problem of overloading. In this case, the execution will be rejected. |
2 |
FATAL_JVM_ERROR This kind of Error Type indicates the occurrence of a fatal error. For example, stack overflow. |
CUSTOM Error Type
自定义错误类型是由我们定义的错误。它们可以在映射或引发错误时定义。我们必须为这些错误类型提供一个特定的自定义命名空间,以便将它们与 Mule 应用程序中的其他现有错误类型区分开来。例如,在使用 HTTP 的 Mule 应用程序中,我们不能使用 HTTP 作为自定义错误类型。
The CUSTOM Error Types are the errors that are defined by us. They can be defined when mapping or when raising the errors. We must give a specific custom namespace to these Error Types for distinguishing them from the other existing Error Types within Mule application. For example, in Mule application using HTTP, we cannot use HTTP as the custom error type.
Categories of Mule Error
广义上讲,Mule 中的错误可以分为两类,即 Messaging Errors and System Errors 。
In broad sense, the errors in Mule can be divided into two categories namely, Messaging Errors and System Errors.
Messaging Error
这一类 Mule 错误与 Mule 流相关。每当 Mule 流中出现问题时,Mule 都会引发消息传递错误。我们可以在错误处理组件内设置 On Error 组件,以处理这些 Mule 错误。
This category of Mule error is related to the Mule flow. Whenever a problem occurs within a Mule flow, Mule throws a messaging error. We can set up On Error component inside the error handler component to handle these Mule errors.
System Error
系统错误表示系统级发生的异常。如果没有 Mule 事件,系统错误将由系统错误处理程序处理。系统错误处理程序处理的异常类型如下所示:
System error indicates an exception occurring at the system level. If there is no Mule event, the system error is handled by a system error handler. The following kind of exceptions handle by a system error handler −
-
Exception that occurs during an application start-up.
-
Exception that occurs when a connection to an external system fails.
如果发生系统错误,Mule 会将错误通知发送给已注册的监听器。它还会记录错误。另一方面,如果错误是由连接失败引起的,Mule 将执行重新连接策略。
In case a system error occurs, Mule sends an error notification to the registered listeners. It also logs the error. On the other hand, Mule executes a reconnection strategy if the error was caused by a connection failure.
Handling Mule Errors
Mule 具有以下两个错误处理程序来处理错误:
Mule has following two Error Handlers for handling the errors −
On-Error Error Handlers
第一个 Mule 错误处理程序是 On-Error 组件,该组件定义了它们可以处理的错误类型。如前所述,我们可以在 ERROR HANDLER 组件(类似范围)内配置 On-Error 组件。每个 Mule 流只包含一个错误处理程序,但该错误处理程序可以包含我们需要的任意多个 On-Error 范围。在流内借助 On-Error 组件处理 Mule 错误的步骤如下:
The first Mule error handler is On-Error component, that defines the types of errors they can handle. As discussed earlier, we can configure On-Error components inside the scope-like Error Handler component. Each Mule flow contain only one error handler, but this error handler can contain as many On-Error scope as we needed. The steps for handling the Mule error inside the flow, with the help of On-Error component, are as follows −
-
First, whenever a Mule flow raises an error, the normal flow execution stops.
-
Next, the process will be transferred to the Error Handler Component that already have On Error component to match the error types and expressions.
-
At last, the Error Handler component routes the error to the first On Error scope that matches the error.

以下是 Mule 支持的两种类型的 On-Error 组件:
Following are the two types of On-Error components supported by Mule −
On-Error Propagate
On-Error Propagate 组件执行,但会将错误传播到下一级并中断所有者的执行。如果它由 On Error Propagate 组件处理,事务将回滚。
On-Error Propagate component executes but propagates the error to the next level and breaks the owner’s execution. The transaction will be rolled back if it is handled by On Error Propagate component.
On-Error Continue
与 On-Error Propagate 组件类似,On-Error Continue 组件也会执行事务。唯一条件是,如果所有者已成功完成执行,则该组件将使用执行结果作为其所有者的结果。如果它由 On-Error Continue 组件处理,事务将提交。
Like On-Error Propagate component, On-Error Continue component also executes the transaction. The only condition is, if the owner had completed the execution successfully then this component will use the result of the execution as the result of its owner. The transaction will be committed if it is handled by On-Error Continue component.
Try Scope Component
Try Scope 是 Mule 4 中众多新功能之一。它的工作方式类似于 JAVA 中的 try 块,在其中我们通常将有异常可能性的代码封装起来,以便可以处理它而不会中断整个代码。
Try Scope is one of many new features available in Mule 4. It works similar to try block of JAVA in which we used to enclose the code having the possibility of being an exception, so that it can be handled without breaking the whole code.
我们可以在 Try Scope 中包装一个或多个 Mule 事件处理器,此后,try scope 将捕获并处理这些事件处理器引发的任何异常。try scope 的主要工作围绕其自己的错误处理策略进行,该策略支持对其内部组件进行错误处理,而不是整个流。因此,我们不需要将流提取到一个单独的流中。
We can wrap one or more Mule event processors in Try Scope and thereafter, try scope will catch and handle any exception thrown by these event processors. The main working of try scope revolves around its own error handling strategy which supports error handling on its inner component instead of whole flow. That is why we do not need to extract the flow into a separate flow.
Example
以下是 try scope 用法的一个示例:
Following is an example of the use of try scope −

Configuring try scope for handling transactions
众所周知,事务是一系列绝不应该部分执行的操作。在事务范围内执行的所有操作都在同一线程中执行,如果发生错误,它将导致回滚或提交。我们可以按以下方式配置 try scope,以便它将子操作视为一个事务。
As we know, a transaction is a series of actions that should never be executed partially. All the operations within the scope of a transaction are executed in the same thread and if an error occurs, it should lead to a rollback or a commit. We can configure the try scope, in the following manner, so that it treats child operations as a transaction.

-
INDIFFERENT [Default] − If we choose this configuration on try block, then the child actions will not be treated as a transaction. In this case, error causes neither rollback nor commits.
-
ALWAYS_BEGIN − It indicates that a new transaction will be started every time the scope is executed.
-
BEGIN_OR_JOIN − It indicates that if the current processing of the flow has already started a transaction, join it. Otherwise, start a new one.