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

adding `wrapErrors` options for wrapping errors into error class #57

Merged
merged 1 commit into from May 21, 2015

Conversation

@xpepermint
Copy link
Contributor

@xpepermint xpepermint commented May 20, 2015

Super simple solution for #51.

Example:

// ./validate.js

var validate = require('validate.js');
var ValidationError = require('../errors/validation_error');

validate.Promise = require('bluebird');
validate.options = validate.async.options = {
  fullMessages: false,
  wrapErrors: ValidationError
};

module.exports = validate;
// ./validation_error

module.exports = function ValidationError(params, message, options) {
  Error.captureStackTrace(this, this.constructor);
  this.name = this.constructor.name;
  this.params = params;
  this.message = message || 'Validation Error';
  this.options = options;
};

require('util').inherits(module.exports, Error);
// ./index.js

var validate = require('./validate');
var ValidationError = require('./validation_error');

var data = {
  name: ''
};
var contraints = {
  name: { presence: true, length: { minimum: 10 } }
};

validate.async(data, contraints).then(function() {
  console.log('ok');
}).catch(ValidationError, function(err) {
  console.log('ValidationError', err);
}).catch(function(err) {
  console.log('SystemError', err);
});

Run as

node index.js
@ansman
Copy link
Owner

@ansman ansman commented May 21, 2015

I like this solution a lot! Super clean and maintains backwards compatibility.

I'll clean it up a bit and add some tests.

ansman added a commit that referenced this pull request May 21, 2015
adding `wrapErrors` options for wrapping errors into error class
@ansman ansman merged commit 259cbee into ansman:master May 21, 2015
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@xpepermint
Copy link
Contributor Author

@xpepermint xpepermint commented May 21, 2015

Cool, thx!

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

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.