> ## 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.

# SDK 錯誤處理最佳做法

> 在不同語言中偵測、檢查和處理管理、伺服器和用戶端 SDK 所傳回之 PlayFab API 錯誤的最佳做法。

本教學課程說明如何使用 PlayFab SDK 存取、辨識和處理 API 錯誤。

此處所述的做法同樣適用於管理、伺服器和用戶端 SDK，但模式*高度*取決於您選擇的語言。

簡單來說，您選擇的模式對任何 SDK (管理/伺服器/用戶端) 都有效，但實作細節將取決於*您的*程式設計語言和環境。

## 攔截和存取錯誤

PlayFab SDK 通常會透過傳回錯誤物件來回報錯誤。下列程式碼片段展示如何偵測和存取錯誤。

```csharp theme={null}
PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest() {
    Email = "doesnotexist@mail.com",
    Password = "nevercorrect",
}, result => {
    // success
}, error => {
    // 'error' object is our point of access to error data
});
```

一般而言，如果錯誤物件已定義 (不是 null)，就表示發生了錯誤。接著我們可以進一步檢查錯誤。

## 檢查錯誤

檢查錯誤最常見的方式，是透過程式碼辨識錯誤。如[全域 API 方法錯誤碼](/zh-TW/services/playfab/api-references/global-api-method-error-codes)中所述，每個產生的錯誤都包含人類可讀的錯誤碼和數字錯誤碼。

<Note>
  錯誤碼*本身*就足以辨識錯誤並據此處理。
</Note>

讓我們以 [LoginWithEmailAddress](xref:titleid.playfabapi.com.client.authentication.loginwithemailaddress) API 方法為例。如此方法的[文件](xref:titleid.playfabapi.com.client.authentication.loginwithemailaddress)中所述，執行時可能會擲回下列內部錯誤：

* `InvalidTitleId 1004`
* `AccountNotFound 1001`
* `InvalidEmailOrPassword 1142`
* `RequestViewConstraintParamsNotAllowed 1303`

下列方法說明如何檢查和辨識此類錯誤。

```csharp theme={null}
PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest() {
    Email = "doesnotexist@mail.com",
    Password = "nevercorrect",
}, result => {
    // success
}, error => {
    // General purpose logging: GenerateErrorReport gives a bunch of information about the error
    Debug.Log(error.GenerateErrorReport());

    // Recognize and handle the error
    switch (error.Error) {
        case PlayFabErrorCode.InvalidTitleId:
            // Handle invalid title id error
            break;
        case PlayFabErrorCode.AccountNotFound:
            // Handle account not found error
            break;
        case PlayFabErrorCode.InvalidEmailOrPassword:
            // Handle invalid email or password error
            break;
        case PlayFabErrorCode.RequestViewConstraintParamsNotAllowed:
            // Handle not allowed view params error
            break;
        default:
            // Handle unexpected error
            break;
    }
});
```

## 處理錯誤

識別錯誤後，處理/復原策略取決於錯誤類型和性質。像*無效引數*這類錯誤，即使重試也永遠不會成功。必須修正要求，該 API 呼叫才會成功。

有一部分錯誤可以套用重試策略。*可重試*的錯誤類型在[全域 API 方法錯誤碼](/zh-TW/services/playfab/api-references/global-api-method-error-codes)中有說明。

套用重試策略時，請務必符合下列需求：

* 每次重試時，重試之間的延遲應以指數方式*增加*。這可以*增加*呼叫成功的機會，並防止您的遊戲對 PlayFab 伺服器發出大量垃圾要求 (這將導致*更多*呼叫被拒絕)。

* 您應*有選擇性地*套用此重試策略，只對值得重試的錯誤碼使用。

如需可安全重試的錯誤碼清單，請參閱我們的[全域 API 方法錯誤碼](/zh-TW/services/playfab/api-references/global-api-method-error-codes)教學課程。


## Related topics

- [呼叫 XBOX 服務的最佳做法](/zh-TW/services/xbox-services/develop/best-practices/live-best-practices-calling-xbl.md)
- [市集錯誤處理](/zh-TW/services/playfab/economy-monetization/economy-v2/marketplace/marketplace-error-handling.md)
- [節流最佳做法](/zh-TW/services/playfab/live-service-management/service-gateway/throttling/best-practices.md)
- [處理離線遊戲的最佳做法](/zh-TW/services/xbox-services/develop/best-practices/live-best-practices-offline-play.md)
- [最佳做法](/zh-TW/services/xbox-services/develop/best-practices/index.md)
