question

John Peterson avatar image
John Peterson asked

Problem with CloudScript handler function returning string.

Dear PlayFab community,

I have run into a weird situation to which I'm hoping to find a solution:

I have the following CloudScript handler (with no arguments) and functions:

var Test = function(args: any, context?: IPlayFabContext): any | never
{
   var result: any = TestNumber(); // TestString(); <-- This will fail!

   return result;
}
handlers["test"] = Test;

function TestNumber(): number | undefined
{
    return 1;
}
function TestString(): string | undefined
{
    return "Test"; // undefined; <-- This will work.
}
function TestObject(): any | undefined
{
   return {Test: "Test"};
}

When I execute "test" in a player context from the Dashboard, I get a SUCCESS when executing TestNumber() or TestObject() with the following information:

{
    "FunctionResult": 1,
    "Logs": [],
    "ExecutionTimeSeconds": 0.0003232,
    "MemoryConsumedBytes": 10256,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": null
}

{
    "FunctionResult": {
        "Test": "Test"
    },
    "Logs": [],
    "ExecutionTimeSeconds": 0.0006081999999999999,
    "MemoryConsumedBytes": 9960,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": null
}

However, if I try to execute TestString(), I get the following ERROR:

{
    "FunctionResult": null,
    "Logs": null,
    "ExecutionTimeSeconds": 0,
    "MemoryConsumedBytes": 0,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": {
        "Error": null,
        "Message": "There was a problem running test. Check your arguments and try again.",
        "StackTrace": null
    }
}

If I change LINE 15 to be:

return undefined;

Then TestString() will be successful.

If anyone has any ideas that I could try, I would be hugely obliged!

Thanks in advance!

apisCloudScript
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

·
John Peterson avatar image
John Peterson answered

I think I was finally able to figure out how to circumvent this issue. Turns out that PlayFab handler functions don't manage a native string return value properly. :-(

If I add the following line before LINE 5 in the original code snippet above, it seems to work as expected:

   if (typeof result === "string") result = {Result: result};

(Basically converting a string to an object.)

I now have a SUCCESS result when invoking TestString() when it returns "Test".

{
    "FunctionResult": {
        "Result": "Test"
    },
    "Logs": [],
    "ExecutionTimeSeconds": 0.0003853,
    "MemoryConsumedBytes": 9600,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": null
}
4 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.

brendan avatar image brendan commented ·

There's actually no custom code to this - it's the standard V8 engine. Glad you managed to find the way to get this to work for you!

0 Likes 0 ·
John Peterson avatar image John Peterson brendan commented ·

Thanks, @Brendan! I'm surprised that you would say that this is a V8 issue and not a PlayFab issue. From everything I'm reading about V8, this absolutely looks like an issue with how PlayFab has integrated with V8. This is such a glaring bug to not be able to return a native string object from the handler...

In any event, I have a workaround (as hacky as it is).

0 Likes 0 ·
brendan avatar image brendan John Peterson commented ·

To be clear, what I'm saying is that we're running a standard V8 engine. The only thing that's different is the addition of the Server API calls, and some pre-defined parameters. Basically, nothing that should impact the ability to return a string. But that said, I should also call out that I did file a bug for investigation.

0 Likes 0 ·
Show more comments

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.