gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

Throw better errors #7

Closed jksdua closed 10 years ago

jksdua commented 10 years ago

Adding a placeholder for updating errors thrown by js-quantities.

Preferably, an instance of Error should be thrown so users can handle them as they usually would other errors.

Additionally, it would be nice to have the args passed to the function printed as part of the message so that there is enough information in the message to be able to debug the issue.

If you agree, I'll send a pull request.

Thanks for the port, the library is great!

Cheers, Jaap

gentooboontoo commented 10 years ago

@jksdua, it sounds like a good idea. I would be glad to merge a pull request from you.

What do you think about implementing a custom Error type QtyError like the following code?

function QtyError() {
  var err = Error.apply(this, arguments);
  this.name = 'QtyError';
  this.message = err.message;
  this.stack = err.stack;
}
QtyError.prototype = Object.create(Error.prototype, {constructor: { value: QtyError }});

It would be semantically correct (This error would be instanceof Error and QtyError).

try {
  .... // Something throwing QtyError instances
}
catch(e) {
  if(e instanceof QtyError) {
    ....
  }
}
jksdua commented 10 years ago

Sounds good. I'll send a pull request on the weekend.