-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |