btrz-circuit-breaker
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "btrz-circuit-breaker", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Circuit Breaker wrapper with native Promises", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test multi='xunit=test-results/xunit.xml spec=-' node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- --ui bdd -R mocha-multi -t 5000", | ||
"test": "NODE_ENV=test node_modules/mocha/bin/mocha --ui bdd -t 5000 --exit", | ||
"test-w": "NODE_ENV=test node_modules/mocha/bin/mocha -w", | ||
"publish": "publish" | ||
"preversion": "npm test", | ||
"postversion": "git push origin master && git push --tags" | ||
}, | ||
@@ -33,8 +34,5 @@ "repository": { | ||
"chance": "0.7.1", | ||
"istanbul": "^0.4.1", | ||
"mocha": "latest", | ||
"mocha-multi": "latest", | ||
"publish": "^0.5.0", | ||
"sinon": "latest" | ||
} | ||
} |
@@ -5,2 +5,6 @@ # btrz-circuit-breaker | ||
# Bump a new version | ||
`npm version patch` | ||
## Engines | ||
@@ -7,0 +11,0 @@ |
@@ -49,4 +49,19 @@ "use strict"; | ||
} else if (response.statusCode >= 400) { | ||
//TODO: improve error thrown | ||
let errorResponse = new Error (`${response.statusCode}: ${response.statusMessage}`); | ||
let parsedBody = null; | ||
let errorResponse = null; | ||
if(body) { | ||
try { | ||
parsedBody = body.toUpperCase ? JSON.parse(body) : body; | ||
errorResponse = new Error (`${response.statusCode}: ${parsedBody.message}`); | ||
errorResponse.code = parsedBody.code; | ||
} catch(err) { | ||
errorResponse = new Error (`${response.statusCode}: ${body}`); | ||
} | ||
} else { | ||
errorResponse = new Error (`${response.statusCode}: ${response.statusMessage}`); | ||
} | ||
errorResponse.status = response.statusCode; | ||
failed(errorResponse); | ||
@@ -53,0 +68,0 @@ reject(errorResponse); |
@@ -108,2 +108,83 @@ /*jshint expr: true*/ | ||
it("should reject with the status code of the response when it fails", function () { | ||
let breaker = new CircuitBreaker(); | ||
let requestLib = makeFakeRequestLib(null, {statusCode: 500, statusMessage: "error"}, JSON.stringify({ | ||
code: 'ERROR_CODE', | ||
message: 'An Error message' | ||
})); | ||
let spy = sinon.spy(requestLib); | ||
let wrappedTransport = breaker.wrapHttpTransport(spy); | ||
let promise = wrappedTransport("get", "http://test"); | ||
return promise | ||
.catch((error) => { | ||
expect(error.status).to.be.equal(500); | ||
}); | ||
}); | ||
it("should reject with the code of the body when it fails", function () { | ||
let breaker = new CircuitBreaker(); | ||
let requestLib = makeFakeRequestLib(null, {statusCode: 500, statusMessage: "error"}, JSON.stringify({ | ||
code: 'ERROR_CODE', | ||
message: 'An Error message' | ||
})); | ||
let spy = sinon.spy(requestLib); | ||
let wrappedTransport = breaker.wrapHttpTransport(spy); | ||
let promise = wrappedTransport("get", "http://test"); | ||
return promise | ||
.catch((error) => { | ||
expect(error.code).to.be.equal('ERROR_CODE'); | ||
}); | ||
}); | ||
it("should reject with the wright message when it fails and the body is parseable", function () { | ||
let breaker = new CircuitBreaker(); | ||
let requestLib = makeFakeRequestLib(null, {statusCode: 500, statusMessage: "error"}, JSON.stringify({ | ||
code: 'ERROR_CODE', | ||
message: 'An Error message' | ||
})); | ||
let spy = sinon.spy(requestLib); | ||
let wrappedTransport = breaker.wrapHttpTransport(spy); | ||
let promise = wrappedTransport("get", "http://test"); | ||
return promise | ||
.catch((error) => { | ||
expect(error.message).to.be.equal('500: An Error message'); | ||
}); | ||
}); | ||
it("should reject with the wright message when it fails and the body is not parseable", function () { | ||
let breaker = new CircuitBreaker(); | ||
let requestLib = makeFakeRequestLib(null, {statusCode: 500, statusMessage: "error"}, | ||
"non-parseable body"); | ||
let spy = sinon.spy(requestLib); | ||
let wrappedTransport = breaker.wrapHttpTransport(spy); | ||
let promise = wrappedTransport("get", "http://test"); | ||
return promise | ||
.catch((error) => { | ||
expect(error.message).to.be.equal('500: non-parseable body'); | ||
}); | ||
}); | ||
it("should reject with the right message when it fails and the body was already parsed by some middleware", function () { | ||
let breaker = new CircuitBreaker(); | ||
let requestLib = makeFakeRequestLib(null, {statusCode: 500, statusMessage: "error"}, { | ||
code: 'ERROR_CODE', | ||
message: 'An Error message' | ||
}); | ||
let spy = sinon.spy(requestLib); | ||
let wrappedTransport = breaker.wrapHttpTransport(spy); | ||
let promise = wrappedTransport("get", "http://test"); | ||
return promise | ||
.catch((error) => { | ||
expect(error.message).to.be.equal('500: An Error message'); | ||
expect(error.code).to.be.equal('ERROR_CODE'); | ||
}); | ||
}); | ||
it("should wrap error http call, rejecting after several attempts", function (done) { | ||
@@ -110,0 +191,0 @@ let breaker = new CircuitBreaker(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18919
5
366
44
6
1