Skip to content

Commit a62d600

Browse files
xenugenio
authored andcommitted
use "throw ..." instead of "Write-Error ...; Exit 1"
Write-Error is (almost) a no-op inside methods. It currently works inside void methods but it's a bug, not an intentional feature. See PowerShell/PowerShell#5331 and PowerShell/PowerShell#9702 Also, "throw" is more correct semantically.
1 parent 7749d70 commit a62d600

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

psperl.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ param (
1313
# Make sure we only attempt to work for PowerShell v5 and greater
1414
# this allows the use of classes.
1515
if ($PSVersionTable.PSVersion.Major -lt 5) {
16-
Write-Error "PowerShell v5.0+ is required for psperl. https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell?view=powershell-6";
17-
exit
16+
throw "PowerShell v5.0+ is required for psperl. https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell?view=powershell-6";
1817
}
1918
$psperl_path = (Split-Path -parent $MyInvocation.MyCommand.Definition);
2019
$env:PSPERL_ROOT = $psperl_path;
@@ -98,16 +97,14 @@ elseif ($install) {
9897

9998
# if nothing is found, a null thingy is returned. This can be tested with a simple truthiness test
10099
if (-Not $found) {
101-
Write-Error("No installable version of Perl found by the name $($install). Try `psperl -available` to get a list of available perl installations.");
102-
exit(1);
100+
throw "No installable version of Perl found by the name $($install). Try `"psperl -available`" to get a list of available perl installations.";
103101
}
104102
# check to see if we got just one.
105103
if ($found -is 'System.Object') {
106104
$psperl.Install($found);
107105
}
108106
else {
109-
Write-Error("Unexpected results $($found.GetType()) searching for $($install). Try `psperl -available` to get a list of available perl installations.");
110-
exit(1);
107+
throw "Unexpected results `"$($found.GetType())`" searching for `"$($install)`". Try `"psperl -available`" to get a list of available perl installations.";
111108
}
112109
}
113110
elseif ($switch) { $psperl.Use($switch, $true); }

src/PSPerl.ps1

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class PSPerl {
77
# Constructor
88
PSPerl([string]$rootPath, [string]$profilePath) {
99
if(![System.IO.Directory]::Exists($rootPath)) {
10-
Write-Error "Invalid root path provided";
11-
exit(1);
10+
throw "Invalid root path provided";
1211
}
1312
$this.URL = 'http://strawberryperl.com/releases.json';
1413
$this.rootPath = $rootPath;
@@ -22,17 +21,15 @@ class PSPerl {
2221
$the_bits = (Get-CimInstance Win32_OperatingSystem).OSArchitecture;
2322
if ($the_bits -eq '64-bit') { return 64; }
2423
elseif ($the_bits -eq '32-bit') { return 32; }
25-
Write-Error("Invalid OS Architecture? We don't know what to do with a $($the_bits)-bit architecture");
26-
exit(1);
24+
throw "Invalid OS Architecture? We don't know what to do with a `"$($the_bits)`" architecture";
2725
}
2826

2927
# return the current PowerShell's bitness. will be 64 or 32. might not be the same as the OS
3028
[int] ArchPS() {
3129
$the_bits = [System.Runtime.InterOpServices.Marshal]::SizeOf([System.IntPtr]::Zero)*8;
3230
if ($the_bits -eq '64') { return 64; }
3331
elseif ($the_bits -eq '32') { return 32; }
34-
Write-Error("Invalid PowerShell Architecture? We don't know what to do with a $($the_bits)-bit PowerShell");
35-
exit(1);
32+
throw "Invalid PowerShell Architecture? We don't know what to do with a $($the_bits)-bit PowerShell";
3633
}
3734

3835
# get an array of available Portable Perl versions
@@ -148,21 +145,18 @@ class PSPerl {
148145
# check to make sure we got the right size file
149146
[int]$size = (Get-Item $pathZip).length;
150147
if ($size -ne $perl_obj.size) {
151-
Write-Error("The file we have is $($size) bytes but we expected $($perl_obj.size). Deleting the file.");
152148
Remove-Item -Path $pathZip -Force;
153-
exit(1);
149+
throw "The file we have is $($size) bytes but we expected $($perl_obj.size). Deleting the file.";
154150
}
155151
# check the SHA1 checksums
156152
[String]$checksum = (Get-FileHash -Path $pathZip -Algorithm SHA1).hash;
157153
if ($checksum -ne $perl_obj.sha1) {
158-
Write-Error("The file's SHA1 checksum is off. Deleting the file.");
159154
Remove-Item -Path $pathZip -Force;
160-
exit(1);
155+
throw "The file's SHA1 checksum is off. Deleting the file.";
161156
}
162157
}
163158
else {
164-
Write-Error("We tried to download the file, but we didn't get it.");
165-
exit(1);
159+
throw "We tried to download the file, but we didn't get it.";
166160
}
167161

168162
# extract the zip into the directory
@@ -252,8 +246,7 @@ class PSPerl {
252246

253247
[void] Use([string]$perl_install, [bool]$persistent = $false) {
254248
if (-Not $this.InstalledPerls().Contains($perl_install)) {
255-
Write-Error("$($perl_install) isn't yet installed. Try installing it.");
256-
exit(1);
249+
throw "$($perl_install) isn't yet installed. Try installing it.";
257250
}
258251
$this.ClearEnvironment();
259252
$env:PATH = "$($this.rootPath);$($env:PATH)";

0 commit comments

Comments
 (0)