Skip to content

Commit

Permalink
print the errors when no package index file could be accessed
Browse files Browse the repository at this point in the history
also change how failures when testing file existence/access are passed
around internally.
  • Loading branch information
jantari committed Dec 1, 2023
1 parent 763b416 commit 47f6b60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
12 changes: 8 additions & 4 deletions private/Get-PackagePathInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'Reachable' = $false
'Type' = 'Unknown'
'AbsoluteLocation' = ''
'ErrorMessage' = ''
}

Write-Debug "Resolving file path '$Path'"
Expand Down Expand Up @@ -82,8 +83,13 @@
}
$response.Dispose()
}
# Catching the (most common) WebException separately just makes the error message nicer as
# it won't have the extra 'Exception calling "GetResponse" with "0" argument(s)' text in it.
catch [System.Net.WebException] {
$PathInfo.ErrorMessage = "URL ${UriToUse} is not reachable: $($_.FullyQualifiedErrorId): $_"
}
catch {
Write-Debug "Could not connect to URL ${UriToUse}: $_"
$PathInfo.ErrorMessage = "URL ${UriToUse} is not reachable: $($_.FullyQualifiedErrorId): $_"
}
}

Expand All @@ -109,9 +115,7 @@
$PathInfo.Type = 'FILE'
$PathInfo.AbsoluteLocation = (Get-Item -LiteralPath $JoinedPath).FullName
} else {
# Writing an error with ErrorAction SilentlyContinue has the purpose of adding it to
# ErrorVariable (and the global $ERROR) but not returning it via the pipeline
Write-Error "'$Path' is not a supported URL and does not exist as a filesystem path" -ErrorAction SilentlyContinue
$PathInfo.ErrorMessage = "'$Path' is not a supported URL and does not exist as a filesystem path"
}
}

Expand Down
12 changes: 9 additions & 3 deletions private/Get-PackagesInRepository.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
$DatabaseXmlPath = Join-Path -Path $Repository -ChildPath "database.xml"
}

if ((Get-PackagePathInfo -Path $ModelXmlPath -TestURLReachable).Reachable) {
# Used as pipeline OutVariables
$ModelXmlPathInfo = $null
$DatabaseXmlPathInfo = $null

if ((Get-PackagePathInfo -Path $ModelXmlPath -TestURLReachable -OutVariable ModelXmlPathInfo).Reachable) {
Write-Verbose "Getting packages from the model xml file ${ModelXmlPath}"
if ($RepositoryType -eq 'HTTP') {
# Model XML method for web based repositories
Expand Down Expand Up @@ -67,7 +71,7 @@
Write-Error "The package definition at $($Package.location) could not be found or accessed"
}
}
} elseif ((Get-PackagePathInfo -Path $DatabaseXmlPath -TestURLReachable).Reachable) {
} elseif ((Get-PackagePathInfo -Path $DatabaseXmlPath -TestURLReachable -OutVariable DatabaseXmlPathInfo).Reachable) {
Write-Debug "Getting packages from the database xml file ${DatabaseXmlPath}"
if ($RepositoryType -eq 'HTTP') {
$webClient = New-WebClient -Proxy $Proxy -ProxyCredential $ProxyCredential -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials
Expand Down Expand Up @@ -114,6 +118,8 @@
Write-Debug "Discovered package $($Package.LocalPath) is not applicable to the computer model and OS"
}
} else {
Write-Warning "The repository '${Repository}' did not contain either a '${Model}_Win10.xml' or 'database.xml' file to get packages from"
Write-Warning "The repository '${Repository}' did not contain either a '${Model}_Win$($CachedHardwareTable._OS).xml' or 'database.xml' file to get packages from"
Write-Warning "Could not get ${Model}_Win$($CachedHardwareTable._OS).xml: $($ModelXmlPathInfo.ErrorMessage)"
Write-Warning "Could not get database.xml: $($DatabaseXmlPathInfo.ErrorMessage)"
}
}
4 changes: 2 additions & 2 deletions public/Get-LSUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@
Write-Warning "Unfortunately, this command produces most accurate results when run as an Administrator`r`nbecause some of the commands Lenovo uses to detect your computers hardware have to run as admin :("
}

$RepositoryInfo = Get-PackagePathInfo -Path $Repository -ErrorVariable InvalidRepositoryReason
$RepositoryInfo = Get-PackagePathInfo -Path $Repository
if (-not $RepositoryInfo.Valid) {
throw "Repository '${Repository}' refers to an invalid location: $InvalidRepositoryReason"
throw "Repository '${Repository}' refers to an invalid location: $($RepositoryInfo.ErrorMessage)"
}

$UTF8ByteOrderMark = [System.Text.Encoding]::UTF8.GetString(@(195, 175, 194, 187, 194, 191))
Expand Down

1 comment on commit 47f6b60

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSScriptAnalyzer results as of this commit:

  • 2 Information
  • 8 Warning
See details
Location : ./private/Split-ExecutableAndArguments.ps1 [1, 10]
RuleName : PSUseSingularNouns
Severity : Warning
Message  : The cmdlet 'Split-ExecutableAndArguments' uses a plural noun. A singular noun
            should be used instead.

Location : ./private/Compare-Array.ps1 [30, 17]
RuleName : PSReviewUnusedParameter
Severity : Warning
Message  : The parameter 'in' has been declared but not used. 

Location : ./private/Debug-LongRunningProcess.ps1 [44, 13]
RuleName : PSAvoidUsingEmptyCatchBlock
Severity : Warning
Message  : Empty catch block is used. Please use Write-Error or throw statements in catc
           h blocks.

Location : ./private/Debug-LongRunningProcess.ps1 [139, 82]
RuleName : PSReviewUnusedParameter
Severity : Warning
Message  : The parameter 'lParam' has been declared but not used. 

Location : ./private/Invoke-PackageCommand.ps1 [303, 29]
RuleName : PSAvoidUsingEmptyCatchBlock
Severity : Warning
Message  : Empty catch block is used. Please use Write-Error or throw statements in catc
           h blocks.

Location : ./private/Resolve-XMLDependencies.ps1 [1, 10]
RuleName : PSUseSingularNouns
Severity : Warning
Message  : The cmdlet 'Resolve-XMLDependencies' uses a plural noun. A singular noun shou
           ld be used instead.

Location : ./private/Set-BIOSUpdateRegistryFlag.ps1 [1, 10]
RuleName : PSUseShouldProcessForStateChangingFunctions
Severity : Warning
Message  : Function 'Set-BIOSUpdateRegistryFlag' has verb that could change system state
           . Therefore, the function has to support 'ShouldProcess'.

Location : ./public/Install-LSUpdate.ps1 [152, 21]
RuleName : PSUseOutputTypeCorrectly
Severity : Information
Message  : The cmdlet 'Install-LSUpdate' returns an object of type 'PackageInstallResult
           ' but this type is not declared in the OutputType attribute.

Location : ./public/Install-LSUpdate.ps1 [189, 21]
RuleName : PSUseOutputTypeCorrectly
Severity : Information
Message  : The cmdlet 'Install-LSUpdate' returns an object of type 'PackageInstallResult
           ' but this type is not declared in the OutputType attribute.

Location : ./public/Set-LSUClientConfiguration.ps1 [1, 10]
RuleName : PSUseShouldProcessForStateChangingFunctions
Severity : Warning
Message  : Function 'Set-LSUClientConfiguration' has verb that could change system state
           . Therefore, the function has to support 'ShouldProcess'.

Please sign in to comment.