Documentation

Did you find this helpful?

Handling Errors in Cloud Script

This guide describes how to recognize and handle errors within your Cloud Script handlers.

Identifying

The first step is identifying the error. While every uncaught error is logged and available from the response to the caller (client), you may still early catch the error by means of a try/catch block. Consider the following Cloud Script snippet that produces and catches the error:

"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
    }
}

Notice how we extract error codes within catch block. Consult with Global API Method Error Codes guide for a complete list of code errors. Code on it's own is sufficient to identify the error.

Logging

Any unhandled error will be added to the response allowing the client to process the problem. At the same time it does create Cloud Script Error entry and is added to the total statistic available on your Cloud Script Dashboard:

To force log the exception in a form of JSON string , use 'error' logging via 'log' object:

"use strict";

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

Finally, you may write Title/Player events for later processing through analytics:

"use strict";

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

Recovery

It's not always possible to recover from errors. Issues like "InvalidArguments" leave you with no option but to report the problem back to the player. 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?