Skip to content

Commit

Permalink
fix: include the error when emitting the 'fallback event'
Browse files Browse the repository at this point in the history
  • Loading branch information
lance committed Mar 2, 2017
1 parent cdc722c commit 40eb2eb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ class CircuitBreaker extends EventEmitter {
* @event CircuitBreaker#reject
*/
this.emit('reject', new Error('Breaker is open'));
return fallback(this, args) || fail(this, 'Breaker is open', args);
return fallback(this, 'Breaker is open', args) ||
fail(this, 'Breaker is open', args);
}
this[PENDING_CLOSE] = this.halfOpen;

Expand All @@ -195,7 +196,7 @@ class CircuitBreaker extends EventEmitter {
* @event CircuitBreaker#timeout
*/
this.emit('timeout', error);
resolve(fallback(this, args) || fail(this, error, args));
resolve(fallback(this, error, args) || fail(this, error, args));
}, this.options.timeout);

const result = this.action.apply(this.action, args);
Expand All @@ -217,28 +218,28 @@ class CircuitBreaker extends EventEmitter {
})
.catch((error) => {
clearTimeout(timeout);
const fb = fallback(this, args);
const fb = fallback(this, error, args);
if (fb) return resolve(fb);
fail(this, error, args);
reject(error);
});
});
} catch (error) {
clearTimeout(timeout);
return fallback(this, args) || fail(this, error, args);
return fallback(this, error, args) || fail(this, error, args);
}
}
}

function fallback (circuit, args) {
function fallback (circuit, err, args) {
if (circuit[FALLBACK_FUNCTION]) {
return new circuit.Promise((resolve, reject) => {
const result = circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], args);
/**
* Emitted when the circuit breaker executes a fallback function
* @event CircuitBreaker#fallback
*/
circuit.emit('fallback', result);
circuit.emit('fallback', result, err);
resolve(result);
});
}
Expand Down

0 comments on commit 40eb2eb

Please sign in to comment.