question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: How can I get error messages from CloudScript in my client code?

I get no error message in my CloudScript JSON file and in the client code when I use this CloudScript to create/update the player's display name. The problem is that I get no error message when the display name already exists. I found out that the display name is still null after running the CloudScript because another player already has exactly the same display name.

I want to show an error message in the client code if a player wants to use a display name that another player already uses. If no other player uses the "DesiredDisplayname", then I want to show the newly created display name to the player. How can I do that?

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "UpdateDisplayname",
    "CloudScriptExecutionResult": {
        "FunctionName": "UpdateDisplayname",
        "Revision": 25,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.0702372,
        "ProcessorTimeSeconds": 0.016,
        "MemoryConsumedBytes": 37424,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 1,
        "Error": null
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "761EA8EC34182A11",
    "EventId": "620f0e43deae4ea8aba6d980016e5c74",
    "SourceType": "BackEnd",
    "Timestamp": "2019-09-20T12:05:11.1056068Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "529e09e"
    }
}

My CloudScript:

handlers.UpdateDisplayname = function (args, context)
{
  var NewDisplayname = args.DesiredDisplayname;
  var CurrentDisplayName = "";
   
   var resultprofile = server.GetPlayerProfile(
       {
           PlayFabID: currentPlayerId, 
           ProfileConstraints : {
               ShowDisplayName: true
               
           }
       });
       
       if (resultprofile.Error == null)
       {
          if (resultprofile.PlayerProfile != null)
          {
            CurrentDisplayName = resultprofile.PlayerProfile.DisplayName;
           
          if ((CurrentDisplayName == null) || (CurrentDisplayName == ""))
           UpdateUserTitleDisplayNameFromCloudScript(NewDisplayname, currentPlayerId);
          else
            log.info("You already have a display name. It's not possible to change it:" + CurrentDisplayName.toString());
          }
       }
}

function UpdateUserTitleDisplayNameFromCloudScript(DesiredDisplayname, PlayFabId)
{
    var contentBodyTemp = {
        "DisplayName": DesiredDisplayname,
        "PlayFabId": PlayFabId
    };
    
    let url = "https://BFD0A.playfabapi.com/Admin/UpdateUserTitleDisplayName";
    let method = "POST";
    let contentBody = `{"PlayFabId": "${PlayFabId}", "DisplayName": "${DesiredDisplayname}"}`;
    let contentType = "application/json";
    let headers = { "X-SecretKey": "..." };
    let responseString = http.request(url, method, contentBody, contentType, headers);
    let responseJSONObj = JSON.parse(responseString);
    return (responseJSONObj.data);
}

Client code:

private async Task UpdateDisplayname()
{
    var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
    {
        FunctionName = "UpdateDisplayname",
        FunctionParameter = new { DesiredDisplayname = "Mynewdisplayname" },
        GeneratePlayStreamEvent = true
    });

    if (result.Error != null)
        Console.WriteLine(result.Error.Error.ToString());
    else
    {
        if (result.Result.Logs.Count() > 0)
            Console.WriteLine(result.Result.Logs[0].Message);
        else
            Console.WriteLine("Your new displayname: " + "Mynewdisplayname");
    }
}
CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

Hi Kim,

You'll need to write an if statement to check the result after updating displayname, error “NameNotAvailable” will occur if the display name already exists. When the display name is properly updated, return it to the client, else when the display name already exists, return error message to the client. You might want to check this doc: Admin/UpdateUserTitleDisplayName https://docs.microsoft.com/en-us/rest/api/playfab/admin/account-management/updateusertitledisplayname?view=playfab-rest

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kim Strasser avatar image Kim Strasser commented ·

I don't know how to do the if statement check in CloudScript. I have already tried to make the if statement check in the client code but this is not working because Result.Error = null in the client code even if the display name is already used by another player:

if (result.Error != null)
{
    if (result.Error.Error == PlayFabErrorCode.NameNotAvailable)
        Console.WriteLine("Another player already has the same display name. Please choose another display name.");
}<br>

How can I check in CloudScript if the display name is available after I called UpdateUserTitleDisplayNameFromCloudScript?

if ((CurrentDisplayName == null) || (CurrentDisplayName == ""))               
    UpdateUserTitleDisplayNameFromCloudScript(NewDisplayname, currentPlayerId);<br>
0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

Hi Kim,

I found the issue that Reuslt.Error = null is due to the function UpdateUserTitleDisplayNameFromCloudScript only returns “data” object to the calling method, which means that error info are eliminated when error occurs. Changing line 43 in Cloud Script to “return (responseJSONObj);” should be able to return error info. The returned result from CloudScript should be like this:

{
    "code": 200,
    "status": "OK",
    "data": {
        "FunctionName": "test",
        "Revision": 110,
        "FunctionResult": {
            "code": 400,
            "status": "BadRequest",
            "error": "NameNotAvailable",
            "errorCode": 1058,
            "errorMessage": "Name not available"
        },

...

    }
}

Now you can check if the display name is available in CloudScript or Client. For instance, in CloudScript the following testing function checks whether display name already exists:

handlers.test = function(args){
     var result =  UpdateUserTitleDisplayNameFromCloudScript(args.NewDisplayname, currentPlayerId);
     if (result.error == "NameNotAvailable")
       	return "display name already exists ";
     return result;   
}
0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Citrus Yan commented ·

Thank you. I get the errors now in my client code and I can show an error message with "result.Result.FunctionResult.ToString()" to the player.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.