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
C
Customer Support
said
almost 7 years ago
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)
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
E
Eric Moncada
said
almost 7 years ago
That's what I was searching for, thanks !
J
Jacques Leemans
said
about 6 years ago
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?
E
Eric Moncada
said
about 6 years ago
Perhaps you can use Spark.exit(); to stop the script without fatal error, and set return code before that.
1 person likes this
J
Jacques Leemans
said
about 6 years ago
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?
Eric Moncada
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 :)
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.
Thanks,
Liam
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
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.
Thanks,
Liam
Eric Moncada
That's what I was searching for, thanks !
Jacques Leemans
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?
Eric Moncada
Perhaps you can use Spark.exit(); to stop the script without fatal error, and set return code before that.
1 person likes this
Jacques Leemans
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?
Tech Support
require("ModuleName");
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics