I'm using Shopify API Node which internally uses GOT for the requests. It exposes hooks configuration which then I can use to modify the behaviour.
Now, When I'd like to retry the request if I've hit the rate limit, I want to retry that request so I've written my logic as below.
{
hooks: {
beforeRequest: [
async () => {
if (
globalVar.limit < 50
) {
console.log('Delaying request by 5 second.')
await sleep(5000) // Just returns the promise which resolves after 5-second
console.log('Request will be made now.')
}
},
],
afterResponse: [
async (response, retryWithMergedOptions) => {
const isRateLimited = true // Conditional based on available cost tracked on global object
return isRateLimited
? retryWithMergedOptions()
: response
},
],
beforeRetry: [
(error, retryCount) => {
console.log(`Retrying [${retryCount}]: ${error.code}`)
if (retryCount > 1) { // Only want to retry it once
console.log('Throwing error')
throw error
}
},
],
beforeError: [
(error) => {
return error // Here I can check if this error is a rate limit error or not, In that case, I want to retry the request otherwise I want to throw the error.
},
],
}
}
Now, I've implemented retry logic in afterResponse
hook but it's not ideal in my situation because Shopify's THROTTLED
response comes as RequestError
thus triggers beforeError
instead of afterResponse
and it seems hard to find how I can retry the request on this case.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too