Skip to content

Commit

Permalink
fix: update the retry function to check the response status correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
christianzoppi committed Jan 18, 2024
1 parent f449960 commit e974e4d
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 e974e4d

Please sign in to comment.