Skip to content

Commit 641d1dd

Browse files
committed
Skip retry on 4XX errors
1 parent 71c5bcf commit 641d1dd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

cloudconfig/templates.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,28 @@ function Start-ExecuteWithRetry {
268268
return $res
269269
} catch [System.Exception] {
270270
$retryCount++
271+
272+
if ($_.Exception -is [System.Net.WebException]) {
273+
$webResponse = $_.Exception.Response
274+
# Skip retry on Error: 4XX (e.g. 401 Unauthorized, 404 Not Found etc.)
275+
if ($webResponse -and $webResponse.StatusCode -ge 400 -and $webResponse.StatusCode -lt 500) {
276+
# Skip retry on 4xx errors
277+
Write-Output "Encountered non-retryable error (4xx): $($_.Exception.Message)"
278+
$ErrorActionPreference = $currentErrorActionPreference
279+
throw
280+
}
281+
}
282+
271283
if ($retryCount -gt $MaxRetryCount) {
272284
$ErrorActionPreference = $currentErrorActionPreference
273285
throw
274286
} else {
275-
if($RetryMessage) {
287+
if ($RetryMessage) {
276288
Write-Output $RetryMessage
277-
} elseif($_) {
289+
} elseif ($_) {
278290
Write-Output $_
279291
}
280-
Start-Sleep $RetryInterval
292+
Start-Sleep -Seconds $RetryInterval
281293
}
282294
}
283295
}

0 commit comments

Comments
 (0)