Sign In Register

How can we help you today?

Start a new topic
Answered

Cloud code question

Hi,


I use cloud code to make some controls and validation stuff.

Is there a way to stop script execution and return a custom error code to the client ?


I use setScriptError, but then the script continues to execute, and I would like to terminate it as soon as I have an error. Tried "return", "break", all these stuff, but nothing works.


I could have lots of nested "if/else", but as my script makes a lot of controls, the code becomes quickly unreadable...


Thanks for your help :)


Best Answer

Hi Eric,


You can do this with "throw" and set a custom error, this will stop the script at it's current point and return the error you have set, this will then be stored in script.log which you can see in the NoSql explorer in the portal. Basic example below. If number = 1 it will throw the error, it will stop the script and you won't get the number in the response. Change the number to any other one, run it again and the script will finish and return the number in the response.


   

var number = 1

if(number == 1){
    throw "Your Error Here";
}

Spark.setScriptData("number", number)

   


Thanks,

Liam




Answer

Hi Eric,


You can do this with "throw" and set a custom error, this will stop the script at it's current point and return the error you have set, this will then be stored in script.log which you can see in the NoSql explorer in the portal. Basic example below. If number = 1 it will throw the error, it will stop the script and you won't get the number in the response. Change the number to any other one, run it again and the script will finish and return the number in the response.


   

var number = 1

if(number == 1){
    throw "Your Error Here";
}

Spark.setScriptData("number", number)

   


Thanks,

Liam



That's what I was searching for, thanks !

Is there a better way to stop execution?


We are busy with a board game, with some rules, I want to stop execution if the user made an invalid move and notify them. At the moment Im using throw, but its causing the game to appear to "fatal" errors.


The code to run on NodeJS, we used "return ERROR_MSG"... can we do something similar in GS?

Perhaps you can use Spark.exit(); to stop the script without fatal error, and set return code before that.


1 person likes this

Thanks Eric, that works perfectly! No mention of this function in the docs... LOL


Side question, do you know how to include modules in RT?

require("ModuleName");

Login to post a comment