Skip to content

Commit

Permalink
fix: type-error on empty action (#528)
Browse files Browse the repository at this point in the history
* When an action is not specified,  through the TypeError

fixes #524
  • Loading branch information
aalykiot authored Jan 7, 2021
1 parent bce3bfc commit 7b51dba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"arrow-parens": ["error", "as-needed"],
"arrow-body-style": ["error", "as-needed"],
"prefer-template": "error",
"max-len": ["warn", { "code": 80 }],
"max-len": ["warn", { "code": 80, "ignoreStrings": true }],
"no-unused-vars": ["warn", {
"argsIgnorePattern": "^_$|^e$|^reject$|^resolve$"
}],
Expand Down
9 changes: 8 additions & 1 deletion lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CircuitBreaker extends EventEmitter {
return !!error[OUR_ERROR];
}

constructor (action = {}, options = {}) {
constructor (action, options = {}) {
super();
this.options = options;
this.options.timeout =
Expand All @@ -128,6 +128,13 @@ class CircuitBreaker extends EventEmitter {

this.semaphore = new Semaphore(this.options.capacity);

// check if action is defined
if (!action) {
throw new TypeError(
'No action provided. Cannot construct a CircuitBreaker without an invocable action.'
);
}

this[VOLUME_THRESHOLD] = Number.isInteger(options.volumeThreshold)
? options.volumeThreshold : 0;
this[WARMING_UP] = options.allowWarmUp === true;
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,16 @@ test('CircuitBreaker without a timeout', t => {
.catch(t.fail);
});

test('CircuitBreaker without an action', t => {
t.plan(1);
try {
// eslint-disable-next-line
const _ = new CircuitBreaker();
} catch (err) {
if (err instanceof TypeError) t.pass();
}
});

const noop = _ => {};
const common = require('./common');
const identity = common.identity;
Expand Down

0 comments on commit 7b51dba

Please sign in to comment.