Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Protect script against wrong roll expressions

1587477024
Laurent
Pro
Sheet Author
API Scripter
Sometimes, I let the user of a script write down their own roll expressions (such as 1d6+2) and then let roll20 evaluate them through sendChat. The issue is, that if the expression is wrong (as in 3+a), then the sandbox will crash. I would rather send an error message and keep the sandbox running. What is the best way to catch such errors? For the record, with expression 3+a, I get Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your scripts and click the "Save Script" button and we'll attempt to start running them again.  More info... For reference, the error message generated was:  Could not determine result type of: [{"type":"M","expr":3},{"type":"C","text":"+a"}] undefined
1587478040
The Aaron
Roll20 Production Team
API Scripter
It's going to depend on the script that is calling sendChat.  It might not be possible to catch depending on where this error happens.  It might require writing an expression parser to try and validate the expression before sending it to the roller.
1587478459
Laurent
Pro
Sheet Author
API Scripter
There is no way to do something like  try { sendChat(rollExpr, function(res) { var rolls = res[0]; .... }); } catch (e) { ... catch Roll20 rollExpr evaluation error ... } ?
1587479120
The Aaron
Roll20 Production Team
API Scripter
Probably not, because the function passed to sendChat is called Asynchronously, the try/catch scope is finished before it gets executed.  If the error occurs at the point of call for sendChat(), that will work, but I suspect it happens somewhere else, which is why the callback is required.  I'd say, give it a try. =D
1587479348
The Aaron
Roll20 Production Team
API Scripter
Actually, that does catch the exception.  So yeah, sprinkle that all over is a good idea!
1587480450
Laurent
Pro
Sheet Author
API Scripter
I should just have tried it out. Thanks Aaron!