Skip to content

Commit aea7392

Browse files
committed
fix bug in retry while error handling
1 parent 22e0756 commit aea7392

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/internal/request.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,34 @@ export async function requestWithRetry(
6565
maxRetries: number = MAX_RETRIES,
6666
): Promise<http.IncomingMessage> {
6767
let attempt = 0
68+
let isRetryable = false
6869
while (attempt <= maxRetries) {
6970
try {
7071
const response = await request(transport, opt, body)
7172
// Check if the HTTP status code is retryable
7273
if (isHttpRetryable(response.statusCode as number)) {
74+
isRetryable = true
7375
throw new Error(`Retryable HTTP status: ${response.statusCode}`) // trigger retry attempt with calculated delay
7476
}
77+
7578
return response // Success, return the raw response
7679
} catch (err) {
77-
attempt++
80+
if (isRetryable) {
81+
attempt++
82+
isRetryable = false
7883

79-
if (attempt > maxRetries) {
80-
throw new Error(`Request failed after ${maxRetries} retries: ${err}`)
84+
if (attempt > maxRetries) {
85+
throw new Error(`Request failed after ${maxRetries} retries: ${err}`)
86+
}
87+
const delay = getExpBackOffDelay(attempt)
88+
// eslint-disable-next-line no-console
89+
console.warn(
90+
`${new Date().toLocaleString()} Retrying request (attempt ${attempt}/${maxRetries}) after ${delay}ms due to: ${err}`,
91+
)
92+
await sleep(delay)
93+
} else {
94+
throw err // re-throw if any request, syntax errors
8195
}
82-
const delay = getExpBackOffDelay(attempt)
83-
// eslint-disable-next-line no-console
84-
// console.warn( `${new Date().toLocaleString()} Retrying request (attempt ${attempt}/${maxRetries}) after ${delay}ms due to: ${err}`,)
85-
await sleep(delay)
8696
}
8797
}
8898

tests/functional/functional-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const assert = chai.assert
4040
const isWindowsPlatform = process.platform === 'win32'
4141

4242
describe('functional tests', function () {
43-
this.timeout(300000) // 5 minutes
43+
this.timeout(30 * 60 * 1000)
4444
var clientConfigParams = {}
4545
var region_conf_env = process.env['MINIO_REGION']
4646

tests/unit/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe('CopyConditions', () => {
253253

254254
describe('Client', function () {
255255
var nockRequests = []
256-
this.timeout(300000) // 5 minutes
256+
this.timeout(5000) // 5 minutes
257257
beforeEach(() => {
258258
Nock.cleanAll()
259259
nockRequests = []

0 commit comments

Comments
 (0)