Skip to content

Commit

Permalink
Merge pull request #754 from storyblok/fix/retry-mechanism-bug
Browse files Browse the repository at this point in the history
fix: update the retry function to check the response status correctly
  • Loading branch information
Thiago Saife authored Jan 18, 2024
2 parents f449960 + e974e4d commit f9c88e9
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,6 @@ class Storyblok {
return new Promise(async (resolve, reject) => {
try {
const res = await this.throttle('get', url, params)
if (res.status === 429) {
retries = retries ? retries + 1 : 0

if (retries < this.maxRetries) {
console.log(`Hit rate limit. Retrying in ${retries} seconds.`)
await this.helpers.delay(1000 * retries)
return this.cacheResponse(url, params, retries)
.then(resolve)
.catch(reject)
}
}

if (res.status !== 200) {
return reject(res)
}
Expand Down Expand Up @@ -640,6 +628,17 @@ class Storyblok {

return resolve(response)
} catch (error: Error | any) {
if (error.response && error.status === 429) {
retries = retries ? retries + 1 : 0

if (retries < this.maxRetries) {
console.log(`Hit rate limit. Retrying in ${retries} seconds.`)
await this.helpers.delay(1000 * retries)
return this.cacheResponse(url, params, retries)
.then(resolve)
.catch(reject)
}
}
reject(error)
}
})
Expand Down

0 comments on commit f9c88e9

Please sign in to comment.