question

Martin L avatar image
Martin L asked

Is there an example of how to force a playfab error callback from a cloudscript?

I am doing a custom purchase and if there are insufficient funds I'd like the script to return via the error callback with an InsufficientFunds (1059) error and not the regular callback. Is it possible to do this and if so how do you do it?

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

·
brendan avatar image
brendan answered

Well first, InsufficientFunds is only returned from a purchase operation, which is called from the client. I believe what you're saying is that you want to do something more complex than a simple purchase (like requiring multiple VCs for the "purchase", or VC+item instance, for example), and that you want to return this error, so with that as the assumption:

There are two schools of thought on this, both of which are valid - it just depends on how you want to manage your title. In both cases, it's important to understand that you're distinguishing the error which occurred in an API call in the script from an error of the script itself running. I realize this may sound like splitting hairs, but it's an important distinction. In terms of how you present the error details to the client code the two recommendations would be:

1. If you do nothing else, any Server API call made in a Cloud Script that receives an error response causes an exception to throw. That will result in all the details of the error being in the response, like this:

{
    "code": 200,
    "status": "OK",
    "data": {
        "FunctionName": "exampleAPIBadRequest",
        "Revision": 1,
        "Logs": [{
            "Level": "Error",
            "Message": "PlayFab API request error",
            "Data": {
                "api": "/Server/UpdateUserStatistics",
                "request": {
                    "PlayFabId": "ADADCB6F9583EA99"
                },
                "result": null,
                "apiError": {
                    "code": 400,
                    "status": "BadRequest",
                    "error": "InvalidUserStatistics",
                    "errorCode": 1073,
                    "errorMessage": "Invalid user statistics",
                    "errorHash": null,
                    "errorDetails": null
                }
            }
        }],
        "ExecutionTimeSeconds": 0.0754191,
        "ProcessorTimeSeconds": 0.016,
        "MemoryConsumedBytes": 122968,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 0,
        "Error": {
            "Error": "CloudScriptAPIRequestError",
            "Message": "The script called a PlayFab API, which returned an error. See the Error logs for details.",
            "StackTrace": "Error\n    at handlers.exampleAPIBadRequest (5F4-main.js:3:12)"
        }
    }
}

Note that in this case, the response code is a 200 (status OK), as the execution of the script completed - it's the logic within the script that had a failure, and so is being reported on.

2. Alternately, you can manage any errors or exceptions in the script using try/throw/catch, and compose your own custom FunctionResult. This would be the way to go if you want to have server-authoritative fallback logic in the script itself.

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

Martin L avatar image Martin L commented ·

Like some others here I am generating custom stats on the fly for each instance. That means I cannot PurchaseItem via the client since there is not a secure way to apply custom stats at purchase time and I have to use a GrantItem/SubtractCurrency pair because there is no Purchase in the server API.

So I would like to return an an InsufficientFunds error exactly like the client Purchase call, ideally via the error callback not the regular cloudscript callback.

0 Likes 0 ·
brendan avatar image brendan Martin L commented ·

Unfortunately, that's not how the return values work for Cloud Script. To put what I said above another way, the issue is that the call to ExecuteCloudScript did not fail, which is why this isn't returned as an error. It's the logic within the script that failed, which in turn is why we provide the method shown above for returning detailed information on the script execution and error(s).

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.