diff --git a/lib/circuit.js b/lib/circuit.js index e534fb9a..d6bb8863 100644 --- a/lib/circuit.js +++ b/lib/circuit.js @@ -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 diff --git a/test/test.js b/test/test.js index 7ed16ab9..b8ba9cbe 100644 --- a/test/test.js +++ b/test/test.js @@ -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;