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

Better error handling for when request function throws #14

Closed
lukechilds opened this issue Oct 16, 2017 · 1 comment
Closed

Better error handling for when request function throws #14

lukechilds opened this issue Oct 16, 2017 · 1 comment

Comments

@lukechilds
Copy link
Collaborator

lukechilds commented Oct 16, 2017

Related: sindresorhus/got#392

When the request function throws (as opposed to emitting an error) it causes cacheable-request to emit an error. This is misleading because errors emitted by cacheable-request are assumed to be caused by a cache error, request errors are handled by listening for the error event on the request object.

However, we can't attach an event listener to the request object in this scenario because the request object wont exist if the request function throws.

We need a clearer way to handle this error.

@lukechilds
Copy link
Collaborator Author

Possible solution: cacheable-request should have two error classes for RequestError and CacheError.

So you could handle all possible errors from cacheable-request like this:

cacheableRequest('example.com', cb)
  .on('error', err => {
    if (err instanceof cacheableRequest.CacheError) {
      handleCacheError(err); // Cache error
    } else if (err instanceof cacheableRequest.RequestError) {
      handleRequestError(err); // Request fn thrown
    }
  })
  .on('request', req => {
    req.on('error', handleRequestError); // Request error emitted
    req.end();
  });

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

No branches or pull requests

1 participant