Sign In Register

How can we help you today?

Start a new topic
Answered

Throw error return with many values

Hi,


The use of of throw in Cloud Code allows us to stop execution and return a string. Is there a way to add more fields to the returned error?


If we throw JSON it shows up as Object in the response.

{
 "@class": ".GameSparksErrorResponse",
 "error": {
  "message": "[object Object] (modules/Guild.js#51)"
 },
 "message": "[object Object] (modules/Guild.js#51)"
}


If I stringify it, then it has ugly escape characters and would need to be parsed.


Ideally we would have more control over the entire response error object. Adding keys

{
 "@class": ".GameSparksErrorResponse",
 "error": {
  "message": "You do not have 100 money.",
  "code" : "MONEY_CANNOT_AFFORD",
  "data" : {"current_money" : 10}
 },
 "message": "you do not have 100 money.",
 "code" : "MONEY_CANNOT_AFFORD",
 "data" : {"current_money" : 10}
}

Best Answer

Hi Christopher,


This new method has now been added. It's called Spark.exit(). There is a brief example of it's usage in this thread. If you have nay further questions just let us know.


Thanks,

Liam


1 person has this question

Hi Christopher,


No you can't do this with a throw. You could do this by setting a ScriptError in your script. You could use something like the following and replace the keys and values with whatever suits


Spark.setScriptError("customError", {"test":"value","test02":"value02","test03":"value03"})

//example response

{
 "@class": ".LogEventResponse",
 "error": {
  "customError": {
   "test": "value",
   "test02": "value02",
   "test03": "value03"
  }
 }
}

 

We will be implementing a new GameSparks method in the coming months that can be used to exit a Cloud Code script at a particular point. At that stage I'd recommend not using throw and switching to that method for all future use. If you have any further questions just let me know.


Thanks,

Liam

Thanks! The new method, will it allow custom error responses? I tried setScriptError but it doesn't show up when used with throw. 

Hi Christopher,


Apologies, I missed your response. I'll find this out and get back to you asap.


Thanks,

Liam

Hi Christopher,


The new method will be used in conjunction with setScriptError. You will use a combination of both to stop the execution of a Cloud Code script and set an error to display in the response when this happens. I'll update the thread when this new method has been released.


Thanks,

Liam


1 person likes this
Answer

Hi Christopher,


This new method has now been added. It's called Spark.exit(). There is a brief example of it's usage in this thread. If you have nay further questions just let us know.


Thanks,

Liam


1 person likes this
Login to post a comment