Skip to content

Commit d8c6afa

Browse files
committed
fix: TerminateWithError was called too late and user config was check too late also
close #21
1 parent 64c2ae5 commit d8c6afa

1 file changed

Lines changed: 44 additions & 47 deletions

File tree

powershell/yt-download.ps1

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,57 @@ param(
1111
# Stop the script if an error occurs.
1212
$ErrorActionPreference = 'Stop'
1313

14-
$ScriptVersion = "2.5.0"
14+
$ScriptVersion = "2.5.0.b1"
15+
16+
function TerminateWithError {
17+
param(
18+
[string]$errorMessage = "Error happened.`nEXIT",
19+
[System.Exception]$exception
20+
)
21+
22+
[console]::beep(1000, 100)
23+
[console]::beep(1000, 100)
24+
[console]::beep(1000, 100)
25+
[console]::beep(1000, 1000)
26+
27+
if ($exception) {
28+
$line = $_.InvocationInfo.ScriptLineNumber
29+
30+
if ($line) {
31+
Write-Host "`n$errorMessage :`n`t$($exception.Message)`n`tLine: $line`nEXIT" -ForegroundColor Red
32+
}
33+
else {
34+
Write-Host "`n$errorMessage :`n$($exception.Message)`nEXIT" -ForegroundColor Red
35+
}
36+
}
37+
else {
38+
Write-Host "ERROR`n" -ForegroundColor Red
39+
Write-Host " $errorMessage`n`nEXIT" -ForegroundColor Red
40+
}
41+
42+
exit 1
43+
}
1544

1645
<#*==========================================================================
1746
* ℹ PARAMETERS
1847
1948
Run the script with the -help parameter to know how to use it
20-
===========================================================================#>
21-
22-
<#*==========================================================================
49+
=============================================================================
2350
* ℹ                  DEFAULT VARIABLES
2451
===========================================================================#>
52+
# import user config
53+
try {
54+
$ConfigPath = Join-Path $PSScriptRoot "config.ps1"
55+
if (Test-Path $ConfigPath) {
56+
. $ConfigPath
57+
}
58+
else {
59+
TerminateWithError -errorMessage "`"config.ps1`" is missing."
60+
}
61+
}
62+
catch {
63+
TerminateWithError -errorMessage "`"config.ps1`" is missing or can not be loaded."
64+
}
2565

2666
# Don’t edit this part unless you know what you do.
2767
# Get default downloads dir for each platform
@@ -53,21 +93,6 @@ else {
5393
}
5494
}
5595

56-
#===========================================================================
57-
# import user config
58-
try {
59-
$ConfigPath = Join-Path $PSScriptRoot "config.ps1"
60-
if (Test-Path $ConfigPath) {
61-
. $ConfigPath
62-
}
63-
else {
64-
TerminateWithError -errorMessage "`"config.ps1`" is missing."
65-
}
66-
}
67-
catch {
68-
TerminateWithError -errorMessage "`"config.ps1`" is missing or can not be loaded."
69-
}
70-
7196
$Defaults = @{
7297
autoAudio = @("https://music.youtube.com/watch?v=")
7398
browserCookies = "firefox"
@@ -174,34 +199,6 @@ function Show-Help {
174199
Write-Host "`t$myCookies`n"
175200
}
176201

177-
function TerminateWithError {
178-
param(
179-
[string]$errorMessage = "Error happened.`nEXIT",
180-
[System.Exception]$exception
181-
)
182-
183-
[console]::beep(1000, 100)
184-
[console]::beep(1000, 100)
185-
[console]::beep(1000, 100)
186-
[console]::beep(1000, 1000)
187-
188-
if ($exception) {
189-
$line = $_.InvocationInfo.ScriptLineNumber
190-
191-
if ($line) {
192-
Write-Host "`n$errorMessage :`n`t$($exception.Message)`n`tLine: $line`nEXIT" -ForegroundColor Red
193-
}
194-
else {
195-
Write-Host "`n$errorMessage :`n$($exception.Message)`nEXIT" -ForegroundColor Red
196-
}
197-
}
198-
else {
199-
Write-Host "ERROR`n" -ForegroundColor Red
200-
Write-Host " $errorMessage`n`nEXIT" -ForegroundColor Red
201-
}
202-
203-
exit 1
204-
}
205202

206203
function WriteTitle {
207204
param(

0 commit comments

Comments
 (0)