Skip to content

Commit

Permalink
wrap DownloadFile call in a try-catch (fix #90)
Browse files Browse the repository at this point in the history
this makes no difference when running Get-LSUpdate in a normal
PowerShell session, but some deployment/script-running solutions wrap
the entire script in a big try-catch which triggered on any .NET
exception from DownloadFile. Try-Catching the DownloadFile call
separately and "downgrading" any exceptions to a PowerShell-native
error prevents any unintended script terminations in such cases.

I still recommend NOT wrapping LSUClient scripts in a global try-catch
whenever it is possible to avoid it.

also referencing previous issues #35, #36, #37 and #65
  • Loading branch information
jantari committed Jul 28, 2023
1 parent a6dacd9 commit 5c0aab3
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion private/Save-PackageFile.ps1
Expand Up @@ -46,7 +46,14 @@
$webClient = New-WebClient -Proxy $Proxy -ProxyCredential $ProxyCredential -ProxyUseDefaultCredentials:$ProxyUseDefaultCredentials

Write-Verbose "Downloading '$($SourceFile.AbsoluteLocation)' to '${DownloadDest}'"
$webClient.DownloadFile($SourceFile.AbsoluteLocation, $DownloadDest)
# Catch all possible .NET Exceptions from this method call and reduce them to a "PowerShell-error" which will respect ErrorAction.
# See: https://github.com/MicrosoftDocs/PowerShell-Docs/issues/1583 and https://github.com/jantari/LSUClient/issues/90
try {
$webClient.DownloadFile($SourceFile.AbsoluteLocation, $DownloadDest)
}
catch {
$PSCmdlet.WriteError($_)
}

return $DownloadDest
} elseif ($SourceFile.LocationType -eq 'FILE') {
Expand Down

1 comment on commit 5c0aab3

@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:

  • 3 Information
  • 8 Warning
See details
Location : ./LSUClient.psm1 [353, 39]
RuleName : TypeNotFound
Severity : Information
Message  : Ignoring 'TypeNotFound' parse error on type 'wmisearcher'. Check if the speci
           fied type is correct. This can also be due the type not being known at parse 
           time due to types imported by 'using' statements.

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 : ./private/Compare-Array.ps1 [30, 17]
RuleName : PSReviewUnusedParameter
Severity : Warning
Message  : The parameter 'in' has been declared but not used. 

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/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/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/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 : ./public/Install-LSUpdate.ps1 [145, 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 [182, 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.