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

Projects
None yet
2 participants
@xpepermint
Contributor

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

This comment has been minimized.

Show comment
Hide comment
@ansman

ansman May 21, 2015

Owner

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

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

Owner

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

Merge pull request #57 from xpepermint/master
adding `wrapErrors` options for wrapping errors into error class

@ansman ansman merged commit 259cbee into ansman:master May 21, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@xpepermint

This comment has been minimized.

Show comment
Hide comment
@xpepermint

xpepermint May 21, 2015

Contributor

Cool, thx!

Contributor

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