Skip to content

Commit

Permalink
feat: pass error as parameter to fallback function (#197)
Browse files Browse the repository at this point in the history
Fixes: #196
  • Loading branch information
hugoduraes authored and lance committed May 24, 2018
1 parent d1b71ab commit ae6c1cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ function handleError (error, circuit, timeout, args, latency, resolve, reject) {
function fallback (circuit, err, args) {
if (circuit[FALLBACK_FUNCTION]) {
const result =
circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], args);
circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], [...args, err]);
/**
* Emitted when the circuit breaker executes a fallback function
* @event CircuitBreaker#fallback
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ test('Executes fallback action, if one exists, when breaker is open', t => {
});
});

test('Passes error as last argument to the fallback function', t => {
t.plan(1);
const fails = -1;
const breaker = circuit(passFail, { errorThresholdPercentage: 1 });
breaker.on('fallback', result => {
t.equals(result, `Error: ${fails} is < 0`, 'fallback received error as last parameter');
t.end();
});
breaker.fallback((x, e) => e);
breaker.fire(fails).catch(t.fail);
});

test('Passes arguments to the fallback function', t => {
t.plan(1);
const fails = -1;
Expand Down

0 comments on commit ae6c1cc

Please sign in to comment.