# ErrorManager 类

## 简要

代表通用客户端、产品客户端的错误管理器的类型。借助错误管理器功能，您可以快速实现对感兴趣的客户端错误处理流程和方式的控制。利用错误管理器所提供的错误处理程序链，您可以编写一个或多个错误处理器函数并对符合特定条件的错误进行处理流程和方式控制，这将进一步增强应用程序可靠性。

```python
class ErrorManager
```

请注意，错误管理器类型的实例化是隐式的且默认集成在所有客户端，您可以通过客户端实例的 `error_manager` 属性访问与之关联的错误管理器实例。

## 属性

错误管理器类型的实例包含可公开访问的属性如下：

| 属性                       | 类型   | 只读 | 描述                                                                  |
| ------------------------ | ---- | -- | ------------------------------------------------------------------- |
| enabled                  | bool | 否  | <p>指示是否已启用错误管理器。<br>将该属性的值设置为 <code>False</code>，将立即禁用错误管理器。</p>    |
| max\_backoff\_interval   | int  | 否  | 指示错误管理器指数退避重试处理方式每次重试的最大间隔秒数。该属性的值默认为 `64` 秒。                       |
| max\_number\_of\_retries | int  | 否  | <p>指示错误管理器重试引发错误的操作时最大可重试的次数。</p><p>该属性的值默认为 <code>10</code> 次。</p> |

## 示例

在下文中我们将通过一段 Python 代码片段向您演示如何向错误管理器添加一个错误处理器：

```python
from tencent.cloud.core import errors
from tencent.cloud.core import client

function_client: functions.Client = functions.Client()

def error_handler_callback(
    error_manager: errors.ErrorManager,
    error_source: client.BaseClient,
    error_instance: errors.ActionError,
    error_retry_count: int
) -> int:
    if not isinstance(error_instance, errors.ActionError):
        return errors.ErrorHandlerResult.Ignore
    
    if error_instance.action_id == 'FailedOperation':
        return errors.ErrorHandlerResult.Backoff
    else:
        return errors.ErrorHandlerResult.Throw

function_client.error_manager.add_handler(error_handler_callback)
```

在上文示例代码中我们定义了一个错误处理器函数 `error_handler_callback`，其参数为：

| 参数                  | 类型                                                                                                                      | 必选 | 描述                 |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------- | -- | ------------------ |
| error\_manager      | [ErrorManager](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/class-and-method/error-manager)  | 是  | 调用错误处理器函数的错误管理器实例。 |
| error\_source       | [BaseClient](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/class-and-method/universal-client) | 是  | 引发该错误的客户端实例。       |
| error\_instance     | [ClientError](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/exceptions/client-error)          | 是  | 引发的错误对应的异常类型实例。    |
| error\_retry\_count | int                                                                                                                     | 是  | 该错误已重试次数。          |

当与之关联的客户端实例引发 [ClientError](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/exceptions/client-error) 异常时，其错误管理器已添加的错误处理器回调函数将根据添加顺序逐个调用。

被回调的错误处理器函数需要返回枚举器 [ErrorHandlerResult](https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/data-types/error-handler-result-enum) 的成员值以指示针对该错误的处理方式，错误管理器将根据返回的处理方式对该错误进行处理。

在上文示例代码中，我们定义并添加的错误处理器代码运行逻辑如下：

![错误处理器运行逻辑示意图](/files/-M14pQYnGzXh7uR0muJC)

## 适用于

#### Tencent Cloud SDK for Python

产品软件包：tencent-cloud-sdk-core >= 0.1.2


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://smallso.gitbook.io/tencent-cloud-sdk/python-docs/other-products/class-and-method/error-manager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
