Skip to content

Commit cfa8e2f

Browse files
committed
(GH-707) Exit with specific exit codes
When running PowerShell and certain things are found, exit appropriately. - When a file is not found, exit 2. - When a file found is not right, exit 4. - When a file can not be downloaded, exit 404. - When unzipping has issues, exit with its result exit code
1 parent d7b4de5 commit cfa8e2f

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ param(
138138
Unregister-Event -SourceIdentifier "LogErrors_ChocolateyZipProc"
139139

140140
$exitCode = $process.ExitCode
141+
Set-PowerShellExitCode $exitCode
141142
$process.Dispose()
142143
Write-Debug "Command ['$7zip' $params] exited with `'$exitCode`'."
143144

src/chocolatey.resources/helpers/functions/Get-WebFile.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ param(
222222
[GC]::Collect()
223223
}
224224

225+
Set-PowerShellExitCode 404
225226
throw "The remote file either doesn't exist, is unauthorized, or is forbidden for url '$url'. $($_.Exception.Message)"
226227
} finally {
227228
if ($res -ne $null) {

src/chocolatey.resources/helpers/functions/Start-ChocolateyProcessAsAdmin.ps1

+6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may t
6161

6262
$exeIsTextFile = [System.IO.Path]::GetFullPath($exeToRun) + ".istext"
6363
if (([System.IO.File]::Exists($exeIsTextFile))) {
64+
Set-PowerShellExitCode 4
6465
throw "The file was a text file but is attempting to be run as an executable - '$exeToRun'"
6566
}
6667

68+
if (!([System.IO.File]::Exists($exeToRun))) {
69+
Set-PowerShellExitCode 2
70+
throw "Could not find '$exeToRun'"
71+
}
72+
6773
# Redirecting output slows things down a bit.
6874
$writeOutput = {
6975
if ($EventArgs.Data -ne $null) {

0 commit comments

Comments
 (0)