Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError on empty action #528

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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