How to catch errors from GlideEvaluator/GlideScopedEvaluator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 05:02 AM
I would like to get/catch errors from evaluated code and store them
Example code:
The example is very short, I did use the GlideEvaluator for this example, although GlideScopedEvaluator has the same issue.
try{
var result = GlideEvaluator.evaluateString("throw Error('stop');");
gs.log('Got Result: ' + result);
} catch(err){
gs.log('Got Error: ' + err);
}
Output (Background Script):
The output shows the error, but thats just the logging. At the bottom you can see 'Got Result: null' which doesn't mean an error, just no return value from the script.
Root cause of JavaScriptException: org.mozilla.javascript.NativeError
JavaScript evaluation error on:
throw Error('stop');
: no thrown error
JavaScript evaluation error on:
throw Error('stop');
: org.mozilla.javascript.JavaScriptException: Error: stop (<refname>; line 1): org.mozilla.javascript.gen._refname__4578._c_script_0(<refname>:1)
org.mozilla.javascript.gen._refname__4578.call(<refname>)
org.mozilla.javascript.gen._refname__4578.exec(<refname>)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:236)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:107)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:73)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:64)
com.glide.script.Evaluator.evaluateString(Evaluator.java:91)
sun.reflect.GeneratedMethodAccessor1891.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:292)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2577)
org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
org.mozilla.javascript.gen.null_null_4581._c_script_0(null.null:2)
org.mozilla.javascript.gen.null_null_4581.call(null.null)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:560)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3439)
org.mozilla.javascript.gen.null_null_4581.call(null.null)
org.mozilla.javascript.gen.null_null_4581.exec(null.null)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:236)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:107)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:73)
com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:324)
com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:285)
com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:256)
com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:244)
com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:321)
com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:216)
com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:174)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:412)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:187)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:165)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:49)
com.glide.sys.ServletTransaction.run(ServletTransaction.java:34)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:748)
*** Script: Got Result: null
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 05:10 AM
Are you talking about capturing the stack trace rather than the error itself?
If yes, this might help:
How to Print Stack Trace via Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 05:15 AM
I need the error itself, or at least know that the evaluator encountered an error. Stacktracing example only works when you have an error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017 07:14 AM
I probably need to look a bit more into GlideEvaluator, see following test that looks strange to me:
var myvar;
try{
myvar = (GlideEvaluator.evaluateString("gs.print('Testing');"));
} catch(e){
gs.print(e);
}
I get:
[0:00:00.004] Script completed in scope global: script
*** Script: Testing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 05:21 PM
Hi there,
It's the context of GlideEvaluator that it is running in.
You could wrap the script string to be evaluated in a try/catch e.g. ...
var myScript = "boo;";
var result = 'yep';
result = GlideEvaluator.evaluateString('try{' + myScript + '}catch(err){ "ERROR! " + err}');
gs.info('Got Result: ' + result);
myScript = "throw Error('stop');";
result = 'yeah';
result = GlideEvaluator.evaluateString('try{' + myScript + '}catch(err){ "EROOR!!! " + err}');
gs.info('Got Result: ' + result);
But seriously, don't do this with production code. You lose control by masking the results.
To paraphrase Peter Lyons, try/catch is bit of a fail.
Read and understand his article - javascript try catch is fail | Peter Lyons
and the article he references - Joyent | Error Handling
... node-js centric but still very valid.
Courtenay