How can i get an error on specific response code natively?

38 views Asked by At

Now this method throws an error if the response code is not 2xx (learned experimentally)

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

(async () => {
  try {
    const response = await request({
      uri: 'http://httpbin.org/status/201',
      resolveWithFullResponse: true,
    });
    console.log(response.statusCode); // we here
  } catch (error) {
    console.error(error); // i want to be here if statusCode !== 200
  }
})();

Maybe there is some parameter or another package that can do something i want?

1

There are 1 answers

3
Ali Beyit On
const request = require('request-promise-native');

(async () => {
  try {
    const response = await request({
      uri: 'http://httpbin.org/status/201',
      resolveWithFullResponse: true,
    });
    console.log(response.statusCode); // we here
    if(response.statusCode !== 200){
        //do your stuff here instead??
    }
  } catch (error) {
    console.error(error); // i want to be here if statusCode !== 200
  }
})();

how about this