> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 CloudScript 中处理错误

> 使用 try/catch 代码块捕获并检查 PlayFab CloudScript 错误,提取 API 错误代码,并从 CloudScript 仪表板监控未处理的错误。

本教程介绍如何识别和处理 CloudScript 处理程序中的错误。

## 识别

第一步是识别错误。虽然每个未捕获的错误都会被记录并且可在响应中提供给调用方(客户端),但你可以通过使用 `try/catch` 代码块尽早捕获错误。

请考虑以下产生并捕获错误的 CloudScript 代码片段。

```javascript theme={null}
"use strict";

handlers.GenerateError = () => {
    try {
        server.GetPlayerStatistics({
            PlayFabId : "non-existing-player-id"
        });
    } catch (ex) {
        let error = ex.apiErrorInfo.apiError.error; // In this case - "InvalidParams"
        let errorCode = ex.apiErrorInfo.apiError.errorCode; // In this case : 1000
    }
}
```

注意在 catch 块中如何提取错误代码?请参阅我们的[全局 API 方法错误代码文档](/services/playfab/api-references/global-api-method-error-codes),获取完整的错误列表。

<Note>
  错误代码本身就足以识别错误。
</Note>

## 日志记录

任何未处理的错误都会被添加到响应中,允许客户端处理该问题。

同时,它会创建一个 CloudScript 错误条目,并将其添加到 CloudScript 仪表板上可用的总统计信息中。

<img src="https://mintcdn.com/microsoft-4404708b/dqv53299jA1M-fNi/images/playfab/live-service-management/service-gateway/automation/cloudscript/tutorials/game-manager-cloudscript-dashboard.png?fit=max&auto=format&n=dqv53299jA1M-fNi&q=85&s=b223b50da990d93023537437490b0ed2" alt="Game Manager - CloudScript 仪表板显示 API 错误的图表" width="1366" height="771" data-path="images/playfab/live-service-management/service-gateway/automation/cloudscript/tutorials/game-manager-cloudscript-dashboard.png" />

若要以 JSON 字符串形式强制记录异常,请通过 `log` 对象使用错误日志记录。

```javascript theme={null}
"use strict";

handlers.GenerateError = () => {
    try {
        server.GetPlayerStatistics({
            PlayFabId : "non-existing-player-id"
        });
    } catch (ex) {
        log.error(ex);
    }
}
```

最后,你可以编写游戏/玩家事件,以便稍后通过分析进行处理。

```javascript theme={null}
"use strict";

handlers.GenerateError = () => {
    try {
        server.GetPlayerStatistics({
            PlayFabId : "non-existing-player-id"
        });
    } catch (ex) {
        server.WriteTitleEvent({
            EventName : 'cs_error',
            Body : ex
        });
    }
}
```

## 恢复

并非总是能够从错误中恢复。诸如 `InvalidArguments` 之类的问题会让你别无选择,只能将问题报告给玩家。

有一部分错误可以应用重试策略。*可重试的*错误类型在[全局 API 方法错误代码](/services/playfab/api-references/global-api-method-error-codes)中进行了描述。

在应用重试策略时,我们请求你*务必*满足以下要求:

* 每次重试时,重试之间的延迟应*呈指数增长*。这会增加成功调用的机会,并防止你的游戏对 PlayFab 服务器发送过多请求(否则会导致*更多*被拒绝的调用)。

* 你应*有选择性地*应用此重试策略,仅将其用于值得重试的错误代码。

## CloudScript 超时错误

CloudScript API 调用的执行时间限制为 4 秒。

如果执行时间超过 4 秒,将引发 `InternalServerError`,并且 PlayStream Event 会写入类似于以下内容的 Logs 对象:

```
    "Logs":[
        {
        "Level":"Error",
        "Message":"PlayFab API request failure",
        "Data":{
            "request":{
                "PlayFabId":"9437A5ADDAE3012D"
            },
            "error":"Timeout",
            "api":"/Server/GetPlayerSegments"
        }
        }
    ]
```

如果你遇到此错误,可以:

* 将你的 CloudScript 拆分为更小的代码段,使其运行时间在 4 秒以内。
* 切换到 [CloudScript 使用 Azure Functions](/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/quickstart),在某些情况下其超时限制更长。你可以在[快速入门指南](/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/quickstart#execution-limits)中查看该限制。


## Related topics

- [PlayFab 在线运营管理文档](/zh-CN/services/playfab/live-service-management/index.md)
- [SDK 错误处理最佳实践](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript/sdk-error-handling-best-practices.md)
- [编写自定义 CloudScript](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript/writing-custom-cloudscript.md)
- [计划任务快速入门](/zh-CN/services/playfab/data-analytics/acting-data/scheduled-tasks/quickstart.md)
- [CloudScript 快速入门](/zh-CN/services/playfab/live-service-management/service-gateway/automation/cloudscript/quickstart.md)
