Documentation

Did you find this helpful?

SDK Error Handling Best Practices

This guide shows how to access, recognize and handle API errors in the SDK. The described practices are equally applicable to Admin, Server and Client SDKs but the patterns depend highly on the language of your choice. In short, the pattern of your choice will be valid for any SDK (Admin/Server/Client) but implementation details will be specific to your programming language and environment.

Catch and access error

PlayFab SDKs usually report errors by returning an error object. The following snippet shows how to detect and access the error:

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

Generally, if 'error' object is defined (not null), this indicates an error has occurred. We may then further inspect the error.

Inspect the error

The most common way is to recognize the error through the code. As described in the Error Codes guide, each generated error contains human-readable and numeric error codes. The code on it's own is sufficient to recognize and process the error accordingly.

Let's take the LoginWithEmailAddress API method as an example. As stated in the documentation page, the following internal errors may be thrown upon execution:

  • InvalidTitleId 1004
  • AccountNotFound 1001
  • InvalidEmailOrPassword 1142
  • RequestViewConstraintParamsNotAllowed 1303

The following method illustrates how to inspect and recognize such errors:

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;
    }
});

Handle the error

Once the error is identified, the handle/recover strategy depends on the error type and nature. Errors like 'invalid arguments' will never succeed if retried. The request must be fixed for that API call to succeed. There are a subset of errors where a retry strategy can be applied. "Retry-able" error types are described in Error Codes guide. Please, make sure to meet the following requirements when applying retry strategy:

  • With each retry, the delay between retries should increase exponentially. This increases your chances for successful call, and prevents your game from spamming the PlayFab server (which will result in more rejected calls)
  • You should apply this retry strategy selectively only to those codes that are worth retrying



Did you find this helpful?