Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Return errors separately #17

Open
ericallam opened this issue Apr 30, 2012 · 2 comments
Open

Feature: Return errors separately #17

ericallam opened this issue Apr 30, 2012 · 2 comments

Comments

@ericallam
Copy link
Contributor

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:

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, data){
  if(err) console.log(err); // ReferenceError: sub is not defined
  console.log(data) // undefined
});

task.run();

Or maybe a different event:

task.on('eval', function(data){
  console.log(data);
});

task.on('error', function(err){
  console.log(err); // ReferenceError: sub is not defined
});

I think I should be able to implement this feature and will submit a pull request when I get a chance.

@rehanift
Copy link
Owner

rehanift commented May 1, 2012

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();

@ericallam
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants