Got: Is possible calls retry on status 200?

Created on 5 Oct 2018  路  4Comments  路  Source: sindresorhus/got

Hi,

I'm making requests using got to a legacy non-RESTful API. This API always respond the requests with HTTP status 200 and the error message within the payload. I tried to do this:

const retry = {
  retries: (retry, error) => {
  // I'd like to do something like this:
 // if (error.body.message !== 'OK') { retry }
},
  methods: ['GET'],
  statusCodes: [200]
};
const requestConfig = {
  json: true,
  baseUrl: 'http://legacyapi/',
  timeout: 1000,
  retry
};
 const response = await got('/failure', requestConfig);

But got never executes the retry and doesn't give any sort of feedback that status 200 isn't allowed on retry statusCodes . Is it possible to do something without implements a back off manually?

Most helpful comment

@alexandrino Would something like this accomplish what you're looking for?

const got = require('got');
const pRetry = require('p-retry');

const requestConfig = {
  json: true,
  baseUrl: 'http://legacyapi/',
  timeout: 1000
};

const response = await pRetry(
  got('/failure', requestConfig)
    .then(response => {
      if (response.body.message !== 'OK') {
        throw new Error(response.body.message);
      }

      return response;
    });
);

All 4 comments

Just an oversight I think.

got currently retires on errors only. Supporting retries on success for non-standard implementations would require a fairly significant refactor. The current implementation does not allow you to "skip" a retry in a manner that would indicate success, which would need to be supported for this.

I'm not sure this is something that should be handled internally given the additional complexity and non-standard behavior.

@alexandrino Would something like this accomplish what you're looking for?

const got = require('got');
const pRetry = require('p-retry');

const requestConfig = {
  json: true,
  baseUrl: 'http://legacyapi/',
  timeout: 1000
};

const response = await pRetry(
  got('/failure', requestConfig)
    .then(response => {
      if (response.body.message !== 'OK') {
        throw new Error(response.body.message);
      }

      return response;
    });
);

@mh81 awesome!!!! That's what I need, thanks a lot for your help!!!

Was this page helpful?
0 / 5 - 0 ratings