Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

adding wrapErrors options for wrapping errors into error class #57

Merged
merged 1 commit into from
May 21, 2015

Conversation

xpepermint
Copy link
Contributor

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 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
@xpepermint
Copy link
Contributor Author

Cool, thx!

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

Successfully merging this pull request may close these issues.

2 participants