Open ericallam opened 12 years ago
This is something I've wanted to do for a while but never got around to doing. I think we need to handle 2 different error scenarios: 1) When the evaluation of the task throws an error (as you noted) and 2) When the task fails to evaluate (ie. network issues, etc.)
What do you think about the below? It would introduce a TaskResponse
value-object to encapsulate all the response data (eval, globals, error info, etc.). I think this would also fit well with the possibly future syntax outlined in issue #14.
var engine = require("engine.js").engine;
var client = engine.client.create();
var task = client.createTask();
task.setContext("(function(locals){ return { add: function(a,b){ return a+b } } })");
task.setLocals({});
task.setCode('sub(1,2)');
task.on('eval', function(err, response){
/*
* err: An Error object that holds information relating to the failed execution
* of running the task. A user can trap this variable when the task fails to run.
*
* response: A TaskResponse object that holds information about the successful execution
* of the task.
* - response.getEvaluation() : The evaluation of the last statement in the task
* - response.isError() : Did the task throw an exception (SyntaxError,
* ReferenceError, SecurityError, etc.)?
* - response.getGlobals() : The context globals (after task execution)
* - response.getLocals() : The context locals (after task execution)
* - ...
* - (other properties can be added later, like execution time, etc. )
*/
});
task.run();
Oooh, yea I like this a lot. It avoids the confusion about the err
parameter, plus it allows for changes to the information returned in the eval
event without changing the API.
It would be nice if it were possible to tell the difference between when code resulted in an error and when it successfully returned a value. I'm thinking of something like this:
Or maybe a different event:
I think I should be able to implement this feature and will submit a pull request when I get a chance.