diff --git a/src/Errors.js b/src/Errors.js deleted file mode 100644 index 4985ed8..0000000 --- a/src/Errors.js +++ /dev/null @@ -1,106 +0,0 @@ -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; - -}; \ No newline at end of file diff --git a/src/Root.js b/src/Root.js index 541d275..92a1f6f 100644 --- a/src/Root.js +++ b/src/Root.js @@ -5,7 +5,7 @@ module.exports = function(GedcomX){ // Extend serialization properties GedcomX.Root.jsonProps.push('features','childAndParentsRelationships', - 'discussions', 'users', 'mergeAnalyses', 'merges'); + 'discussions', 'users', 'mergeAnalyses', 'merges', 'errors'); // Override init() so that we can deserialize normalized values var oldInit = GedcomX.Root.prototype.init; @@ -18,6 +18,7 @@ module.exports = function(GedcomX){ this.setUsers(json.users); this.setMergeAnalyses(json.mergeAnalyses); this.setMerges(json.merges); + this.setErrors(json.errors); } }; @@ -249,4 +250,42 @@ module.exports = function(GedcomX){ return this.merges || []; }; + /** + * Get the errors + * + * @function getMerges + * @instance + * @memberof Root + * @return {Error[]} + */ + GedcomX.Root.prototype.getErrors = function(){ + return this.errors || []; + }; + + /** + * Set the errors + * + * @function getMerges + * @instance + * @memberof Root + * @param {Error[]} errors + * @returns {Root} this + */ + GedcomX.Root.prototype.setErrors = function(errors){ + return this._setArray(errors, 'errors', 'addError'); + }; + + /** + * Add a errors + * + * @function getMerges + * @instance + * @memberof Root + * @param {Error} error + * @returns {Root} this + */ + GedcomX.Root.prototype.addError = function(error){ + return this._arrayPush(error, 'errors', GedcomX.Error); + }; + }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 370aca3..df529b1 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,6 @@ 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); diff --git a/test/Errors.js b/test/Errors.js deleted file mode 100644 index ce35a95..0000000 --- a/test/Errors.js +++ /dev/null @@ -1,46 +0,0 @@ -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); -} \ No newline at end of file diff --git a/test/Root.js b/test/Root.js index b39ed68..90b8bf8 100644 --- a/test/Root.js +++ b/test/Root.js @@ -293,4 +293,37 @@ describe('Root GedcomX property extensions', function(){ }); + describe('errors', function(){ + + 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}" + } ] + }; + + it('Create with JSON', function(){ + test(GedcomX(json)); + }); + + it('Build', function(){ + test(GedcomX().addError(new GedcomX.Error(json.errors[0]))); + }); + + it('toJSON', function(){ + assert.deepEqual(GedcomX(json).toJSON(), json); + }); + + function test(gedx){ + assert.equal(gedx.getErrors().length, 1); + var error = gedx.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); + } + + }); + }); \ No newline at end of file