Best way to implement "retry on 404" #1781
-
What would you like to discuss?For internal business use, I interact with a closed source, third party Restful API. Performances are good but stability was very fine until recently. In a nutshell, this Rest API server sometimes get into a confused state and returns an http 404 response even though the request is valid and result exists. We currently identified 2 workarounds:
To implement the second workaround, an {
afterResponse: [
async (response: Response, retryWithMergedOptions) => {
if (response.statusCode === 404) {
const updatedOptions: ExtendOptions = {};
await resetConnectionPool(); // restore API server
return retryWithMergedOptions(updatedOptions);
}
// No changes otherwise
return response;
},
],
} Yet I am unsure how to best implement a "retry on 404 only once" rule, to prevent the unlikely/rare occasions where 404 is the correct response. Thus my questions are:
TIA Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
To manage the state you can use |
Beta Was this translation helpful? Give feedback.
-
Given that const defaultOptions: ExtendOptions = {
hooks: {
afterResponse: [
async (response, retryWithMergedOptions) => {
const {statusCode} = response;
if (statusCode === 404) {
const reset = await resetConnectionPool();
console.error('!!!', 'resetConnectionPool', reset.statusCode);
return retryWithMergedOptions({});
}
return response;
},
],
},
pagination: {paginate, transform, stackAllItems: false},
responseType: 'json',
...authOptions,
};
return got.extend(defaultOptions); |
Beta Was this translation helpful? Give feedback.
-
Amazing, but I am wondering how would this approach be in a different scenario: There is an external API which has a access_token, and after some time the The idea is that, when external API throws a 401 error, instead of throwing an error without the data output, we could get the new AC by performing a login via a HTTP request and then with the new AC (refreshed token) retry the request and send the data output. What would you recommend? Is it possible? |
Beta Was this translation helpful? Give feedback.
Given that
afterResponse
is not triggered byretryWithMergedOptions
, the solution is in fact straightforward and dead simple: