Skip to content

Commit a7b0b3c

Browse files
Add verbose logging to Invoke-HttpRequestWithRetry (#530)
Adds Write-Verbose logging to Invoke-HttpRequestWithRetry so HTTP request details and errors are visible when running with -Verbose. ## Changes - Log request method, URI, retry config, timeout, and download path on entry - Log each attempt number - Log transient status codes before retrying - Log final status code on success with SkipHttpErrorCheck - Log download completion with file path - Log status code and exception message on errors
1 parent 99dfb71 commit a7b0b3c

13 files changed

Lines changed: 61 additions & 23 deletions

src/ALZ/Private/Config-Helpers/Edit-ALZConfigurationFilesInPlace.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function Edit-ALZConfigurationFilesInPlace {
4545
if ($bicepConfigNode -is [array]) {
4646
# If this is an array - use the property as an array index...
4747
if ($propertyNames[$index] -match "[0-9]+" -eq $false) {
48+
Write-ToConsoleLog "Configuration specifies an array, but the index value '${$propertyNames[$index]}' is not a number. Property path: $($propertyNames -join '.')" -IsError
4849
throw "Configuration specifies an array, but the index value '${$propertyNames[$index]}' is not a number"
4950
}
5051

src/ALZ/Private/Config-Helpers/Get-ALZConfig.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function Get-ALZConfig {
99
)
1010

1111
if (!(Test-Path $configFilePath)) {
12-
Write-Error "The config file does not exist at $configFilePath"
12+
Write-ToConsoleLog "The config file does not exist at $configFilePath" -IsError
1313
throw "The config file does not exist at $configFilePath"
1414
}
1515

@@ -25,7 +25,7 @@ function Get-ALZConfig {
2525
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Yaml -Ordered)
2626
} catch {
2727
$errorMessage = "Failed to parse YAML inputs. Please check the YAML file for errors and try again. $_"
28-
Write-Error $errorMessage
28+
Write-ToConsoleLog $errorMessage -IsError
2929
throw $errorMessage
3030
}
3131

@@ -34,18 +34,19 @@ function Get-ALZConfig {
3434
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Json)
3535
} catch {
3636
$errorMessage = "Failed to parse JSON inputs. Please check the JSON file for errors and try again. $_"
37-
Write-Error $errorMessage
37+
Write-ToConsoleLog $errorMessage -IsError
3838
throw $errorMessage
3939
}
4040
} elseif ($extension -eq ".tfvars") {
4141
try {
4242
$config = [PSCustomObject](& $hclParserToolPath $configFilePath | ConvertFrom-Json)
4343
} catch {
4444
$errorMessage = "Failed to parse HCL inputs. Please check the HCL file for errors and try again. $_"
45-
Write-Error $errorMessage
45+
Write-ToConsoleLog $errorMessage -IsError
4646
throw $errorMessage
4747
}
4848
} else {
49+
Write-ToConsoleLog "Unsupported config file extension '$extension' for file '$configFilePath'. Supported extensions: .json, .yml, .yaml, .tfvars" -IsError
4950
throw "The config file must be a json, yaml/yml or tfvars file"
5051
}
5152

src/ALZ/Private/Config-Helpers/Set-Config.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function Set-Config {
7878
if($inputConfigItemValue.Length -le $index) {
7979
Write-Verbose "Input config item $($inputConfigName) does not have an index of $index."
8080
if($index -eq 0) {
81-
Write-Error "At least one value is required for input config item $($inputConfigName)."
81+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
8282
throw "At least one value is required for input config item $($inputConfigName)."
8383
}
8484
} else {
@@ -87,7 +87,7 @@ function Set-Config {
8787
} catch {
8888
Write-Verbose "Error accessing index $index for input config item $($inputConfigName): $_"
8989
if($index -eq 0) {
90-
Write-Error "At least one value is required for input config item $($inputConfigName)."
90+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
9191
throw "At least one value is required for input config item $($inputConfigName)."
9292
}
9393
}
@@ -98,7 +98,7 @@ function Set-Config {
9898
} else {
9999
Write-Verbose "Input config item $($inputConfigName) with index $index is null."
100100
if($index -eq 0) {
101-
Write-Error "At least one value is required for input config item $($inputConfigName)."
101+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
102102
throw "At least one value is required for input config item $($inputConfigName)."
103103
}
104104
}
@@ -111,7 +111,7 @@ function Set-Config {
111111
$mapItem = $inputConfigItemValue.PsObject.Properties | Where-Object { $_.Name -eq $indexString }
112112
} catch {
113113
Write-Verbose "Error accessing map item $indexString for input config item $($inputConfigName): $_"
114-
Write-Error "At least one value is required for input config item $($inputConfigName)."
114+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
115115
throw "At least one value is required for input config item $($inputConfigName)."
116116
}
117117
if($null -ne $mapItem) {
@@ -121,17 +121,17 @@ function Set-Config {
121121
continue
122122
} else {
123123
Write-Verbose "Input config item $($inputConfigName) with index $indexString is null."
124-
Write-Error "At least one value is required for input config item $($inputConfigName)."
124+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
125125
throw "At least one value is required for input config item $($inputConfigName)."
126126
}
127127
} else {
128128
Write-Verbose "Input config item $($inputConfigName) does not have an index of $indexString."
129-
Write-Error "At least one value is required for input config item $($inputConfigName)."
129+
Write-ToConsoleLog "At least one value is required for input config item $($inputConfigName)." -IsError
130130
throw "At least one value is required for input config item $($inputConfigName)."
131131
}
132132
}
133133
} else {
134-
Write-Error "Input config item $($inputConfigName) not found."
134+
Write-ToConsoleLog "Input config item $($inputConfigName) not found." -IsError
135135
throw "Input config item $($inputConfigName) not found."
136136
}
137137
}

src/ALZ/Private/Deploy-Accelerator-Helpers/Copy-ParameterFileCollection.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Copy-ParametersFileCollection {
2121
Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse -Force | Out-String | Write-Verbose
2222
}
2323
} else {
24-
Write-Warning "The file $sourcePath does not exist."
24+
Write-ToConsoleLog "The file $sourcePath does not exist." -IsWarning
2525
}
2626
}
2727
}

src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Invoke-Terraform {
4646
}
4747

4848
if ($null -eq $subscriptionId -or $subscriptionId -eq "") {
49-
Write-Error "Subscription ID not found. Please ensure you are logged in to Azure and have selected a subscription, or provide bootstrap_subscription_id. Use 'az account show' to check."
49+
Write-ToConsoleLog "Subscription ID not found. Please ensure you are logged in to Azure and have selected a subscription, or provide bootstrap_subscription_id. Use 'az account show' to check." -IsError
5050
return
5151
}
5252
$env:ARM_SUBSCRIPTION_ID = $subscriptionId

src/ALZ/Private/Deploy-Accelerator-Helpers/New-ModuleSetup.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function New-ModuleSetup {
8989
$shouldDownload = $false
9090

9191
if($isAutoVersion -and $upgrade.IsPresent -and $null -eq $latestReleaseTag) {
92+
Write-ToConsoleLog "Cannot perform upgrade to latest version of '$targetFolder' as unable to determine latest release from GitHub. Current version: $currentVersion" -IsError
9293
throw "Cannot perform upgrade to latest version as unable to determine latest release from GitHub."
9394
}
9495

src/ALZ/Private/Deploy-Accelerator-Helpers/Request-ALZConfigurationValue.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function Request-ALZConfigurationValue {
170170
# Load the schema file
171171
$schemaPath = Join-Path $PSScriptRoot "AcceleratorInputSchema.json"
172172
if (-not (Test-Path $schemaPath)) {
173-
Write-Warning "Schema file not found at $schemaPath. Proceeding without descriptions."
173+
Write-ToConsoleLog "Schema file not found at $schemaPath. Proceeding without descriptions." -IsWarning
174174
$schema = $null
175175
} else {
176176
$schema = Get-Content -Path $schemaPath -Raw | ConvertFrom-Json

src/ALZ/Private/Shared/Get-GithubReleaseTag.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ function Get-GithubReleaseTag {
7272
Write-Verbose "Status code: $statusCode"
7373

7474
if ($statusCode -eq 404) {
75-
Write-Error "The release $release does not exist in the GitHub repository $githubRepoUrl - $repoReleaseUrl"
75+
Write-ToConsoleLog "The release $release does not exist in the GitHub repository $githubRepoUrl - $repoReleaseUrl. HTTP status code: $statusCode" -IsError
7676
throw "The release $release does not exist in the GitHub repository $githubRepoUrl - $repoReleaseUrl"
7777
}
7878

7979
if ($statusCode -ne 200) {
80+
Write-ToConsoleLog "Unable to query repository version from $repoReleaseUrl. HTTP status code: $statusCode" -IsError
8081
throw "Unable to query repository version, please check your internet connection and try again..."
8182
}
8283

src/ALZ/Private/Shared/Get-OsArchitecture.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Get-OSArchitecture {
3434
)
3535

3636
if($supportedOsAndArchitectures -notcontains $osAndArchitecture) {
37-
Write-Error "Unsupported OS and architecture combination: $osAndArchitecture"
37+
Write-ToConsoleLog "Unsupported OS and architecture combination: $osAndArchitecture" -IsError
3838
return
3939
}
4040

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,14 @@ function Invoke-HttpRequestWithRetry {
124124
$commonParams["Headers"] = $Headers
125125
}
126126

127+
Write-Verbose "HTTP $Method $Uri (MaxRetries=$MaxRetryCount, RetryInterval=${RetryIntervalSeconds}s$(if ($PSBoundParameters.ContainsKey('TimeoutSec')) { ", Timeout=${TimeoutSec}s" })$(if ($isDownload) { ", OutFile=$OutFile" }))"
128+
127129
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
130+
Write-Verbose "HTTP $Method $Uri - Attempt $attempt of $maxAttempts"
128131
try {
129132
if ($isDownload) {
130133
Invoke-WebRequest @commonParams -OutFile $OutFile
134+
Write-Verbose "HTTP $Method $Uri - Download complete (saved to $OutFile)"
131135
return
132136
}
133137

@@ -137,11 +141,14 @@ function Invoke-HttpRequestWithRetry {
137141
$code = [int]$response.StatusCode
138142

139143
if ($code -in $transientStatusCodes -and $attempt -lt $maxAttempts) {
140-
Write-Warning "Request to $Uri returned status $code (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..."
144+
Write-Verbose "HTTP $Method $Uri - Transient status $code on attempt $attempt"
145+
Write-ToConsoleLog "Request to $Uri returned status $code (attempt $attempt of $maxAttempts). Retrying in $RetryIntervalSeconds seconds..." -IsWarning
141146
Start-Sleep -Seconds $RetryIntervalSeconds
142147
continue
143148
}
144149

150+
Write-Verbose "HTTP $Method $Uri - Completed with status $code"
151+
145152
if ($ReturnStatusCode) {
146153
return @{
147154
Result = $response
@@ -161,10 +168,33 @@ function Invoke-HttpRequestWithRetry {
161168

162169
$isTransient = $responseCode -in $transientStatusCodes
163170

171+
Write-Verbose "HTTP $Method $Uri - Error on attempt ${attempt}: Status=$responseCode, Message=$($_.Exception.Message)"
172+
164173
if ($isTransient -and $attempt -lt $maxAttempts) {
165-
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
166175
Start-Sleep -Seconds $RetryIntervalSeconds
167176
} 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+
Write-Verbose "Failed to read response body: $($_.Exception.Message)"
195+
}
196+
}
197+
Write-ToConsoleLog $errorDetails -IsError
168198
throw
169199
}
170200
}

0 commit comments

Comments
 (0)