Skip to content

Commit

Permalink
add ability to construct an error using an options variable
Browse files Browse the repository at this point in the history
  • Loading branch information
itai-codefresh committed Jul 26, 2016
1 parent c7bce1d commit a6b8121
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 48 deletions.
Empty file added README.md
Empty file.
42 changes: 31 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ var WError = require('verror').WError;
class CFError extends WError {
constructor(errorType, cause, errorMsg) { // jshint ignore:line
var args = Array.prototype.slice.call(arguments);
errorType = args.shift();
var options;
if (args.length === 1){
options = args[0];
errorType = options.type;
}
else {
errorType = args.shift();
}

if (errorType) {
if (typeof errorType === "string" && !CFError.errorTypes[errorType]) {
throw new WError(CFError.errorTypes.WrongInputError, 'The error type is incorrect');
Expand All @@ -18,7 +26,27 @@ class CFError extends WError {
throw new WError(CFError.errorTypes.WrongInputError, 'The error type is missing');
}

super(...args);

if (options){
if (options.cause){
super(options.cause, options.message);
}
else {
super(options.message);
}
if (options.hasOwnProperty("recognized")){
if (options.recognized){
this.recognized = "true";
}
else{
this.recognized = "false";
}
}
}
else {
super(...args);
}


if (errorType === CFError.errorTypes.Inherit){
if (this.we_cause && this.we_cause.name){
Expand Down Expand Up @@ -65,15 +93,7 @@ class CFError extends WError {
getStatusCode() {
return CFError.errorCodes[this.name];
}

recognize() {
this.recognized = "true";
}

deRecognize() {
this.recognized = "false";
}


isRecognized() {
if (this.recognized === "false"){
return false;
Expand Down
113 changes: 76 additions & 37 deletions test.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,54 @@ chai.use(sinonChai);

describe('CFErrors tests', function () {

it('Creating a new CFError should succeed', function () {
var error = new CFError(ErrorTypes.Error, "test");
expect(error).to.exists; // jshint ignore:line
describe('constructor', function(){

it('Creating a new CFError should succeed', function () {
var error = new CFError(ErrorTypes.Error, "test");
expect(error).to.exists; // jshint ignore:line
});

});

describe('recognize tests', function(){

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

it('should recognize in case there is only 1 CFError in the chain', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
expect(error.isRecognized()).to.equal(true);
});

it('should recognize in case recognize was called and then deRecognized and then recognized again on the same error', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
error.deRecognize();
error.recognize();
var error = new CFError({
type: ErrorTypes.Error,
message: "my error",
recognized: true
});
expect(error.isRecognized()).to.equal(true);
});

it('should recognize in case a previous error deRecognized and the recent one recognized', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.deRecognize();
var error2 = new CFError(ErrorTypes.Inherit, error, "extend error");
error2.recognize();
var error = new CFError({
type: ErrorTypes.Error,
message: "my error",
recognized: false
});
var error2 = new CFError({
type: ErrorTypes.Inherit,
cause: error,
message: "extended error",
recognized: true
});
expect(error2.isRecognized()).to.equal(true);
});

it('should recognize in case previous error recognized and the recent one did not do anything', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
var error2 = new CFError(ErrorTypes.Inherit, error, "extend error");
var error = new CFError({
type: ErrorTypes.Error,
message: "my error",
recognized: true
});
var error2 = new CFError({
type: ErrorTypes.Inherit,
cause: error,
message: "extended error"
});
expect(error2.isRecognized()).to.equal(true);
});

Expand All @@ -50,34 +63,60 @@ describe('CFErrors tests', function () {
describe('negative', function(){

it('should not recognize in case there is only 1 CFError in the chain and recognize function was not called', function(){
var error = new CFError(ErrorTypes.Error, "my error");
var error = new CFError({
type: ErrorTypes.Error,
message: "error"
});
expect(error.isRecognized()).to.equal(false);
});

it('should not recognize in case a higher error have deRecognized after a previous error recognized', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
var error2 = new CFError(ErrorTypes.Error, error, "extended error");
error2.deRecognize();
var error = new CFError({
type: ErrorTypes.Error,
message: "error",
recognized: true
});
var error2 = new CFError({
type: ErrorTypes.Error,
cause: error,
message: "extended Error",
recognized: false
});
expect(error2.isRecognized()).to.equal(false);
});

it('should not recognize in case recognized was called and then deRecognized was called on the same error', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
error.deRecognize();
var error = new CFError({
type: ErrorTypes.Error,
message: "error",
recognized: false
});
expect(error.isRecognized()).to.equal(false);
});

it('should not recognize in case recognized was called and then deRecognized was called on the same error', function(){
var error = new CFError(ErrorTypes.Error, "my error");
error.recognize();
error.deRecognize();
error.recognize();
error.deRecognize();
expect(error.isRecognized()).to.equal(false);
});
});

});

describe('using util string output', function(){

it('should print expected message', function(){
var error = new CFError(ErrorTypes.Error, "this is: %s my :%d", "string", 2);
expect(error.message).to.equal("this is: string my :2");
});

});

describe('using EC6 string with params', function(){

it('should print expected message', function(){
var s = "string";
var d = 2;
var error = new CFError({
type: ErrorTypes.Error,
message: `this is: ${s} my :${d}`
});
expect(error.message).to.equal("this is: string my :2");
});

});
Expand Down

0 comments on commit a6b8121

Please sign in to comment.