You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP →

common-errors

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

common-errors - npm Package Compare versions

Comparing version

to
0.4.16

var generateClass = require('./helpers/class-generator');
module.exports = generateClass("ValidationError", {
args: ['message', 'code']
});
var ArgumentError = require('./argument');
var ValidationError = module.exports = generateClass("ValidationError", {
args: ['message', 'code', 'field']
});
ValidationError.prototype.addError = function addError(error) {
this.errors = this.errors || [];
this.errors.push(error);
return this;
}
ValidationError.prototype.addErrors = function addErrors(errors) {
if(!(errors instanceof Array)) throw new ArgumentError("errors");
this.errors = this.errors || [];
Array.prototype.push.apply(this.errors, errors);
return this;
}
ValidationError.prototype.generateMessage = function generateMessage(){
return this.message || "Validation failed.";
}
ValidationError.prototype.toJSON = function toJSON(){
var o = {};
if(this.errors) {
if(this.message) o.message = this.message;
o.errors = this.errors.map(function(error){
return error.toJSON();
});
} else {
if(this.message) o.text = this.message;
if(this.code) o.code = this.code;
if(this.field) o.field = this.field;
}
return o;
}

@@ -5,3 +5,3 @@ {

"description": "Common error classes and utility functions",
"version": "0.4.15",
"version": "0.4.16",
"repository": {

@@ -8,0 +8,0 @@ "type": "git",

@@ -514,4 +514,5 @@ common-errors

Useful for denoting a problem with a user-defined value. Generally, you won't throw this error.
It serializes to JSON, and it can also function as an envelope for multiple errors.
new ValidationError(message[, code])
new ValidationError(message, [code], [field])

@@ -522,3 +523,9 @@ __Arguments__

* `code` - an optional error code
* `field` - an optional description of the data
__Methods__
* `addError(error)` - add an error object to the `errors` array, and return `this`.
* `addErrors(errors)` - append an array of error objects to the `errors` array, and return `this`.
```js

@@ -528,4 +535,4 @@ // Example

var errors = [];
if(username.length < 3) errors.push(new errors.ValidationError("username must be at least two characters long", "VAL_MIN_USERNAME_LENGTH"));
if(/-%$*&!/.test(username)) errors.push(new errors.ValidationError("username may not contain special characters", "VAL_USERNAME_SPECIALCHARS"));
if(username.length < 3) errors.push(new errors.ValidationError("username must be at least two characters long", "VAL_MIN_USERNAME_LENGTH", "username"));
if(/-%$*&!/.test(username)) errors.push(new errors.ValidationError("username may not contain special characters", "VAL_USERNAME_SPECIALCHARS", "username"));
return errors;

@@ -532,0 +539,0 @@ }

@@ -8,8 +8,55 @@ var assert = require('assert');

it("should work", function(){
var error = new Err("This is a validation message.", 'VAL_CODE_1');
var error = new Err("This is a validation message.", 'VAL_CODE_1', 'name');
assert.equal(error.name, name), 'Its name is correct.';
assert.equal(error.message, "This is a validation message.");
assert.equal(error.field, "name");
assert.deepEqual(error.toJSON(), {"text":"This is a validation message.","field":"name","code":"VAL_CODE_1"});
assert.ok(new RegExp(error.name + ": " + error.message + "\n(.*\n)+").test(error.stack), "Stack is good");
assert.ok(error instanceof Error, "It is an instanceof Error");
});
describe("nested errors", function(){
it("should addError", function(){
var error = new Err().addError(new Err("This is a validation message.", 'VAL_CODE_1', 'name'));
assert.equal(error.name, name), 'Its name is correct.';
assert.equal(error.message, "Validation failed.");
assert.deepEqual(error.toJSON(), {
message: "Validation failed.",
errors: [{
"text":"This is a validation message.",
"field":"name",
"code":"VAL_CODE_1"
}]
});
assert.ok(new RegExp(error.name + ": " + error.message + "\n(.*\n)+").test(error.stack), "Stack is good");
assert.ok(error instanceof Error, "It is an instanceof Error");
});
it("should addErrors", function(){
var error = new Err().addErrors([new Err("This is a validation message.", 'VAL_CODE_1', 'name')]);
assert.equal(error.name, name), 'Its name is correct.';
assert.equal(error.message, "Validation failed.");
assert.deepEqual(error.toJSON(), {
message: "Validation failed.",
errors: [{
"text":"This is a validation message.",
"field":"name",
"code":"VAL_CODE_1"
}]
});
assert.ok(new RegExp(error.name + ": " + error.message + "\n(.*\n)+").test(error.stack), "Stack is good");
assert.ok(error instanceof Error, "It is an instanceof Error");
});
it("should require an Array for addErrors", function(){
var caught_error;
try {
var error = new Err().addErrors(1);
} catch(e) {
caught_error = e;
}
assert.ok(caught_error instanceof errors['ArgumentError'])
});
});
});