Skip to content

Commit

Permalink
feat: add ETIMEDOUT error code for timeout error (#64)
Browse files Browse the repository at this point in the history
Added an error code ETIMEDOUT to easily identify timeout error at caller
site. Previously, we need to do checking with e.g. `indexOf('Timed out') >= 0`.
  • Loading branch information
Dhi Aurrahman authored and lance committed Jun 6, 2017
1 parent f482485 commit 5df9f65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class CircuitBreaker extends EventEmitter {
() => {
timeoutError = true;
const error = new Error(`Timed out after ${this.options.timeout}ms`);
error.code = 'ETIMEDOUT';
/**
* Emitted when the circuit breaker action takes longer than `options.timeout`
* @event CircuitBreaker#timeout
Expand Down
8 changes: 6 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ test('Fails when the circuit function fails', (t) => {
});

test('Fails when the circuit function times out', (t) => {
t.plan(1);
t.plan(2);
const expected = 'Timed out after 10ms';
const expectedCode = 'ETIMEDOUT';
const breaker = cb(slowFunction, { timeout: 10 });

breaker.fire()
.then(t.fail)
.catch((e) => t.equals(e.message, expected, 'timeout message received'))
.catch((e) => {
t.equals(e.message, expected, 'timeout message received');
t.equals(e.code, expectedCode, 'ETIMEDOUT');
})
.then(t.end);
});

Expand Down

0 comments on commit 5df9f65

Please sign in to comment.