Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ETIMEDOUT error code for timeout error #64

Merged
merged 1 commit into from
Jun 6, 2017

Conversation

diorahman
Copy link

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.

This is useful when we need to abort or cancel the underlying process. The following contrived example tries to depict the usefulness of having ETIMEDOUT as error.code for when breaker timed out.

const circuitBreaker = require('./');

let handle;

function asyncTimeout () {
  return new Promise((resolve) => {
    handle = setTimeout(() => {
      console.log('timeout!');
      resolve();
    }, 1000);
  });
}

const breaker = circuitBreaker(asyncTimeout, { timeout: 500 });
breaker.fire()
  .then(console.log)
  .catch((err) => {
    // this is very naive but, imagine we have access to abort function for the underlying process
    // without this, the asyncTimeout will be fired.
    if (err.code === 'ETIMEDOUT') { // rather than e.message.indexOf('Timed out') === 0
      clearTimeout(handle);
    }
    console.error(err);
  });

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`.
@coveralls
Copy link

coveralls commented May 31, 2017

Coverage Status

Coverage increased (+0.003%) to 99.278% when pulling 1f23114 on diorahman:timeout-error-code into 727e803 on bucharest-gold:master.

@lance lance self-assigned this Jun 2, 2017
@lance lance merged commit 5df9f65 into nodeshift:master Jun 6, 2017
@lance lance removed the needs-review label Jun 6, 2017
@lance
Copy link
Member

lance commented Jun 6, 2017

@diorahman thanks for the contribution

@diorahman
Copy link
Author

@lance 🎉 BTW, when is the next release?

@lance
Copy link
Member

lance commented Jun 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants