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

feat: pass circuit params to error filter #492

Merged
merged 1 commit into from
Nov 23, 2020
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 lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ class CircuitBreaker extends EventEmitter {
function handleError (error, circuit, timeout, args, latency, resolve, reject) {
clearTimeout(timeout);

if (circuit.options.errorFilter(error)) {
if (circuit.options.errorFilter(error, ...args)) {
circuit.emit('success', error, latency);
} else {
fail(circuit, error, args, latency);
Expand Down
22 changes: 21 additions & 1 deletion test/error-filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const options = {
resetTimeout: 10000,
// if this function returns true, the error statistics
// should not be incremented
errorFilter: err => err.statusCode < 500
errorFilter: (err) => err.statusCode < 500
};

test('Bypasses failure stats if errorFilter returns true', t => {
Expand Down Expand Up @@ -54,3 +54,23 @@ test('Increments failure stats if no filter is provided', t => {
t.end();
});
});

test('Provides invocation parameters to error filter', t => {
t.plan(3);
const errorCode = 504;
const breaker = new CircuitBreaker(failWithCode,
{
errorThresholdPercentage: 1,
errorFilter: (err, param) => {
t.equal(param, errorCode);
t.equal(err.statusCode, errorCode);
}
}
);
breaker.fire(errorCode)
.then(t.fail)
.catch(err => {
t.equal(err.statusCode, errorCode);
t.end()
});
});