Skip to content

Commit

Permalink
add generic ability to get first value of nested errors
Browse files Browse the repository at this point in the history
  • Loading branch information
itai-codefresh committed Aug 17, 2016
1 parent e375087 commit 8f8c851
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
40 changes: 15 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,6 @@ var CFError = function CFError() {
}
});
this.stack = tempStack;


var tempRecognized = this.recognized;
Object.defineProperty(this, 'recognized', {
get: function () {
if (this._recognized === true) {
return true;
}
else if (this._recognized === false) {
return false;
}

if (this.cause && this.cause.hasOwnProperty("recognized")) {
return this.cause.recognized;
}
else {
return false;
}
},
set: function (value) {
this._recognized = value;
}
});
this.recognized = tempRecognized;

};

util.inherits(CFError, Error);
Expand All @@ -122,6 +97,21 @@ CFError.prototype.toString = function () {
return (str);
};

CFError.prototype.getFirstValue = function (field) {
if (this.hasOwnProperty(field.toString())){
return this[field];
}
else if (this.cause && this.cause instanceof CFError){
return this.cause.getFirstValue(field);
}
else if (this.cause) {
return this.cause[field];
}
else {
return undefined;
}
};


CFError.Errors = Errors;

Expand Down
29 changes: 20 additions & 9 deletions lib/tests/test.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('CFErrors tests', function () {

});

describe('recognized', function () {
describe('getFirstValue', function () {

describe('positive - should return true', function () {

Expand All @@ -213,7 +213,7 @@ describe('CFErrors tests', function () {
message: "my error",
recognized: true
});
expect(error.recognized).to.equal(true);
expect(error.getFirstValue("recognized")).to.equal(true);
});

it('should recognize in case a previous error deRecognized and the recent one recognized', function () {
Expand All @@ -228,7 +228,7 @@ describe('CFErrors tests', function () {
message: "extended error",
recognized: true
});
expect(error2.recognized).to.equal(true);
expect(error2.getFirstValue("recognized")).to.equal(true);
});

it('should recognize in case previous error recognized and the recent one did not do anything', function () {
Expand All @@ -242,7 +242,7 @@ describe('CFErrors tests', function () {
cause: error,
message: "extended error"
});
expect(error2.recognized).to.equal(true);
expect(error2.getFirstValue("recognized")).to.equal(true);
});

it('should not call handle correctly case when base error is not CFError', function () {
Expand All @@ -252,7 +252,7 @@ describe('CFErrors tests', function () {
cause: error,
message: "extended error"
});
expect(error2.recognized).to.equal(false);
expect(error2.getFirstValue("recognized")).to.equal(undefined);
});

it('should not call handle correctly case when base error is not CFError', function () {
Expand All @@ -263,7 +263,18 @@ describe('CFErrors tests', function () {
message: "extended error",
recognized: true
});
expect(error2.recognized).to.equal(true);
expect(error2.getFirstValue("recognized")).to.equal(true);
});

it('should return value of a field that was set on a non CFError object', function(){
var err = new Error("my first error");
err.recognized = "my value";

var error = new CFError({
cause: err,
message: "my message"
});
expect(error.getFirstValue("recognized")).to.equal("my value");
});

});
Expand All @@ -275,7 +286,7 @@ describe('CFErrors tests', function () {
name: Errors.Error.name,
message: "error"
});
expect(error.recognized).to.equal(false);
expect(error.getFirstValue("recognized")).to.equal(undefined);
});

it('should not recognize in case a higher error have deRecognized after a previous error recognized', function () {
Expand All @@ -290,7 +301,7 @@ describe('CFErrors tests', function () {
message: "extended Error",
recognized: false
});
expect(error2.recognized).to.equal(false);
expect(error2.getFirstValue("recognized")).to.equal(false);
});

it('should not recognize in case recognized was called and then deRecognized was called on the same error', function () {
Expand All @@ -299,7 +310,7 @@ describe('CFErrors tests', function () {
message: "error",
recognized: false
});
expect(error.recognized).to.equal(false);
expect(error.getFirstValue("recognized")).to.equal(false);
});

});
Expand Down

0 comments on commit 8f8c851

Please sign in to comment.