Skip to content

Commit

Permalink
feat: pass circuit params to error filter (#492)
Browse files Browse the repository at this point in the history
When the circuit invocation fails, and there is an error filter in place,
this change provides the function invocation parameters as the second
parameter to `errorFilter()`.

Fixes: #489

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance authored Nov 23, 2020
1 parent f365918 commit 29175d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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()
});
});

0 comments on commit 29175d7

Please sign in to comment.