Skip to content

Commit

Permalink
Merge pull request #5 from codefresh-io/CF-2504
Browse files Browse the repository at this point in the history
Cf 2504
  • Loading branch information
itai-codefresh committed Jul 26, 2016
2 parents cb86c5f + 5dea45d commit b30243c
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 48 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

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 b30243c

Please sign in to comment.