Skip to content

Commit 73255a5

Browse files
committed
Use Write-ToConsoleLog for warnings and errors in Invoke-HttpRequestWithRetry
- Replace Write-Warning with Write-ToConsoleLog -IsWarning for retry messages - Add Write-ToConsoleLog -IsError with detailed error info before throwing - Include status code, exception message, and response body in error output
1 parent 04f082f commit 73255a5

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/ALZ/Private/Shared/Invoke-HttpRequestWithRetry.ps1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function Invoke-HttpRequestWithRetry {
142142

143143
if ($code -in $transientStatusCodes -and $attempt -lt $maxAttempts) {
144144
Write-Verbose "HTTP $Method $Uri - Transient status $code on attempt $attempt"
145-
Write-Warning "Request to $Uri returned status $code (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..."
145+
Write-ToConsoleLog "Request to $Uri returned status $code (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..." -IsWarning
146146
Start-Sleep -Seconds $RetryIntervalSeconds
147147
continue
148148
}
@@ -171,9 +171,30 @@ function Invoke-HttpRequestWithRetry {
171171
Write-Verbose "HTTP $Method $Uri - Error on attempt $attempt: Status=$responseCode, Message=$($_.Exception.Message)"
172172

173173
if ($isTransient -and $attempt -lt $maxAttempts) {
174-
Write-Warning "Request to $Uri failed with status $responseCode (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..."
174+
Write-ToConsoleLog "Request to $Uri failed with status $responseCode (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..." -IsWarning
175175
Start-Sleep -Seconds $RetryIntervalSeconds
176176
} else {
177+
$errorDetails = "HTTP $Method $Uri failed after $attempt attempt(s)."
178+
if ($null -ne $responseCode) {
179+
$errorDetails += " Status code: $responseCode."
180+
}
181+
$errorDetails += " Error: $($_.Exception.Message)"
182+
if ($_.Exception.Response) {
183+
try {
184+
$stream = $_.Exception.Response.GetResponseStream()
185+
if ($null -ne $stream) {
186+
$reader = [System.IO.StreamReader]::new($stream)
187+
$responseBody = $reader.ReadToEnd()
188+
$reader.Dispose()
189+
if (-not [string]::IsNullOrWhiteSpace($responseBody)) {
190+
$errorDetails += " Response body: $responseBody"
191+
}
192+
}
193+
} catch {
194+
# Ignore errors reading response body
195+
}
196+
}
197+
Write-ToConsoleLog $errorDetails -IsError
177198
throw
178199
}
179200
}

0 commit comments

Comments
 (0)