From a6b812141859f8d9161e2b71111e9e34d09cde40 Mon Sep 17 00:00:00 2001 From: Itai Gendler Date: Tue, 26 Jul 2016 16:47:32 +0300 Subject: [PATCH 1/2] add ability to construct an error using an options variable --- README.md | 0 index.js | 42 ++++++++++++----- test.unit.spec.js | 113 +++++++++++++++++++++++++++++++--------------- 3 files changed, 107 insertions(+), 48 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js index e84c6e9..5a2fac9 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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){ @@ -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; diff --git a/test.unit.spec.js b/test.unit.spec.js index 8ce8533..9e4fbf4 100644 --- a/test.unit.spec.js +++ b/test.unit.spec.js @@ -7,9 +7,13 @@ 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(){ @@ -17,31 +21,40 @@ describe('CFErrors 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); }); @@ -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"); }); }); From 5dea45d0cedafa474c8f56dafda5fc0e0937e60e Mon Sep 17 00:00:00 2001 From: Itai Gendler Date: Tue, 26 Jul 2016 17:10:36 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index e69de29..90febaf 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,65 @@ +[![Coverage Status](https://coveralls.io/repos/github/codefresh-io/cf-errors/badge.svg?branch=develop)](https://coveralls.io/github/codefresh-io/cf-errors?branch=develop) + +# Codefresh erroring library +This library is a wrapper of Verror library that adds additional beheaviour dedicated for our use. + +## Basic Usage - creating an error +```javascript +var CFError = require('cf-errors'); +var ErrorTypes = CFError.errorTypes; + +var error = new CFError( + { + type: ErrorType.Error, + message: `error message` + } +); +``` + +## Advanced Usage - extending an already existing error +```javascript +var extendedError = new CFError( + { + type: ErrorType.Error, + message: `extended error message`, + cause: error + } +); +``` + +### Inheriting the previous error type +In order to be able to extend the previous error with more data without affecting the type of the error is possible using the Inherited error type +```javascript +var extendedError = new CFError( + { + type: ErrorType.Inherit, + message: `extended error message`, + cause: error + } +); +``` + +### signifying an error as a recognized error +Sometimes it is important to be able to differentiate between an error that is recognized and between an un-recognized error. +For example in case your api receives a request that has a missing parameter you would like to create an error but not report it back to your monitoring systems like new-relic. +Then in your error middleware you can check if the error has been recognized and act accordingly and not report this error to your monitoring systems. +```javascript +var extendedError = new CFError( + { + type: ErrorType.Inherit, + message: `extended error message`, + cause: error, + recognized: true + } +); +``` +Then you can check if the error is recognized: +```javascript +if (error.isRecognized()){ + //Your code +} +``` +The default return value of isRecognized function will be false. +Signifying an error as a recognized error will affect the whole chain of errors and will be inherited. +This also means that you can signify a higher error explicitly with the value false which will then make the isRecognized function return false. +