Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

btrz-circuit-breaker

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

btrz-circuit-breaker - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

10

package.json
{
"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"
}
}

4

README.md

@@ -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();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc