Skip to content

Commit

Permalink
Errors list; close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Nov 3, 2016
1 parent f0db148 commit 63fb46a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module.exports = function(GedcomX){

/**
* A list of errors.
*
* @class Errors
* @extends Base
* @param {Object} [json]
*/
var Errors = function(json){

// Protect against forgetting the new keyword when calling the constructor
if(!(this instanceof Errors)){
return new Errors(json);
}

// If the given object is already an instance then just return it. DON'T copy it.
if(Errors.isInstance(json)){
return json;
}

this.init(json);
};

Errors.prototype = Object.create(GedcomX.Base.prototype);

Errors._gedxClass = Errors.prototype._gedxClass = 'GedcomX.Errors';

Errors.jsonProps = [
'errors'
];

/**
* Check whether the given object is an instance of this class.
*
* @memberof Errors
* @static
* @param {Object} obj
* @returns {Boolean}
*/
Errors.isInstance = function(obj){
return GedcomX.utils.isInstance(obj, this._gedxClass);
};

/**
* Initialize from JSON
*
* @memberof Errors
* @param {Object}
* @return Errors this
*/
Errors.prototype.init = function(json){

GedcomX.Base.prototype.init.call(this, json);

if(json){
this.setErrors(json.errors);
}
return this;
};

/**
* Get the errors
*
* @memberof Errors
* @return {Error[]}
*/
Errors.prototype.getErrors = function(){
return this.errors || [];
};

/**
* Set the errors
*
* @memberof Errors
* @param {Error[]} errors
* @returns {Errors} this
*/
Errors.prototype.setErrors = function(errors){
return this._setArray(errors, 'errors', 'addError');
};

/**
* Add a errors
*
* @memberof Errors
* @param {Error} error
* @returns {Errors} this
*/
Errors.prototype.addError = function(error){
return this._arrayPush(error, 'errors', GedcomX.Error);
};

/**
* Export the object as JSON
*
* @memberof Errors
* @return {Object} JSON object
*/
Errors.prototype.toJSON = function(){
return this._toJSON(GedcomX.Base, Errors.jsonProps);
};

GedcomX.Errors = Errors;

};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function(GedcomX){
require('./Discussion')(GedcomX);
require('./DiscussionReference')(GedcomX);
require('./Error')(GedcomX);
require('./Errors')(GedcomX);
require('./FeatureSet')(GedcomX);
require('./FeedbackInfo')(GedcomX);
require('./MatchInfo')(GedcomX);
Expand Down
46 changes: 46 additions & 0 deletions test/Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var assert = require('chai').assert,
GedcomX = require('gedcomx-js');

var json = {
"errors" : [ {
"code" : 401,
"message" : "Unable to read tf person.",
"stacktrace" : "GET http://tf.prod.us-east-1.prod.fslocal.org/tf/person/KWC8-LKC?oneHops=none returned a response status of 401 Unauthorized:\n{\n\"401\": \"Unauthorized\"\n}"
} ]
};

describe('Errors', function(){

it('Create plain', function(){
assert.instanceOf(new GedcomX.Errors(), GedcomX.Errors, 'An instance of Errors is not returned when calling the constructor with new.');
assert.instanceOf(GedcomX.Errors(), GedcomX.Errors, 'An instance of Errors is not returned when calling the constructor without new.');
});

it('Create with JSON', function(){
test(GedcomX.Errors(json));
});

it('Build', function(){
test(GedcomX.Errors().addError(new GedcomX.Error(json.errors[0])));
});

it('toJSON', function(){
assert.deepEqual(GedcomX.Errors(json).toJSON(), json);
});

it('constructor does not copy instances', function(){
var obj1 = GedcomX.Errors();
var obj2 = GedcomX.Errors(obj1);
assert.strictEqual(obj1, obj2);
});

});

function test(errors){
assert.equal(errors.getErrors().length, 1);
var error = errors.getErrors()[0];
assert.equal(error.getCode(), json.errors[0].code);
assert.equal(error.getLabel(), json.errors[0].label);
assert.equal(error.getMessage(), json.errors[0].message);
assert.equal(error.getStacktrace(), json.errors[0].stacktrace);
}

0 comments on commit 63fb46a

Please sign in to comment.