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

Emit central error event from Vorpal #94

Open
paglias opened this issue Feb 6, 2016 · 4 comments
Open

Emit central error event from Vorpal #94

paglias opened this issue Feb 6, 2016 · 4 comments

Comments

@paglias
Copy link

paglias commented Feb 6, 2016

Would it be possible to have an event that catches all errors so we can respond accordingly?

For example every time I throw an error in command.validate() (fyi, it's not documented in the wiki) or command.action() I want to log the error somewhere and output a friendly error message.

@dthree
Copy link
Owner

dthree commented Feb 6, 2016

Wouldn't you want to handle the error based on the context of the command?

I mean, I can have vorpal emit a global duplicate event of error, but I wouldn't want to suppress local errors.

Could you please explain a bit more? Thanks.

@paglias
Copy link
Author

paglias commented Feb 6, 2016

@dthree thanks for the quick response!

I'm used (in web apps) to have a global error handler and I think it would be useful for vorpal too:

command.action((args, callback) => {
  asyncOp(err => {
    // Do something with the error, maybe try to identify the cause, ...
   throw error; // error could be a specific error (NotFound()) or something more general
  })
};
vorpal.on('error', (error, context) {
   // Context would be similar to {command: commandName, args: actionArguments}
   // Print an error message and log the error in a separate file or maybe close the app...
})

This way you can have a central place where all error are handled so you can have a standardized handling and don't have to duplicate logic. This error handler should be passed the same context as the command that originated the error so that it could do things like this.log and access the command's args

@paglias
Copy link
Author

paglias commented Feb 6, 2016

You could do something similar by having an function to handle errors and call it like this:

command.action(async (args) => {
   // error happened
   return handleError(err, args, this);
})

but it would require to wrap all the code in try/catch code to correctly route errors to the specific handler

@dthree
Copy link
Owner

dthree commented Feb 6, 2016

Okay I guess I'm okay with this:

vorpal.on('error', (error, context) {
   // Context would be similar to {command: commandName, args: actionArguments}
   // Print an error message and log the error in a separate file or maybe close the app...
})

BTW, when you programmatically execute Vorpal commands, this is already covered:

vorpal.exec('foo').then(function (data) {
  // didn't error
}).catch(function (err) {
  // errored
});

vorpal.exec('foo', function (err, data) {
  if (err) {

  }
});
@dthree dthree changed the title Error event Mar 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants