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

Catch with errors.StatusCodeError doesn't work if a generic exception is thrown #52

Open
articice opened this issue Aug 22, 2019 · 1 comment

Comments

@articice
Copy link

When using native promises .catch() with error filter, if the function with received body throws an error, the constructor of the error itself throws another error.

TypeError: Cannot set property 'name' of undefined
at StatusCodeError (/home/user/project/node_modules/request-promise-core/lib/errors.js:24:15)

Using this require syntax

const request = require('request-promise-native');
const errors = require('request-promise-native/errors');

consider the following snippet, when the REST API returns status='error':

return request(options)
      .then((reply) => {
        if (reply.status === 'ok') {
          return (reply.data.user.email === user.email);
        } else {
          throw new Error(reply.message || 'Authentication service error');
        }
      }).catch(errors.StatusCodeError, err => {
        if (err.statusCode === 401) {
          console.error('AuthService rejected our authentication settings');
        } else throw err;
      });

NodeJS: 12.8.0
npm: 6.10.2

@articice
Copy link
Author

The same code snippet works for Bluebird version of request-promise.

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