Skip to content

Commit 00f9bf5

Browse files
Merge pull request #42 from theohbrothers/fix/fix-error-handling-to-not-throw-exceptions-on-any-failure-of-any-xaction-scripts-xrotate-scripts-or-rotation
Fix: Fix error handling to not throw exceptions on any failure of any *action scripts, *rotate scripts, or rotation
2 parents f746fed + 214ba39 commit 00f9bf5

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/Log-Rotate/private/rotate/Process-Local-Block.ps1

+1-8
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ function Process-Local-Block {
219219
Start-Script $firstaction $blockpath -ErrorAction $CallerEA
220220
}catch {
221221
Write-Error "Failed to run firstaction script for $blockpath." -ErrorAction Continue
222-
throw
223222
}
224223
}
225224

@@ -234,7 +233,6 @@ function Process-Local-Block {
234233
$log.PrePrerotate()
235234
}catch {
236235
Write-Error "Failed to rotate log $($log['logfile'].FullName)." -ErrorAction Continue
237-
throw
238236
}
239237
}
240238

@@ -246,7 +244,6 @@ function Process-Local-Block {
246244
Start-Script $prerotate $blockpath -ErrorAction $CallerEA
247245
}catch {
248246
Write-Error "Failed to run shared prerotate script for $blockpath. " -ErrorAction Continue
249-
throw
250247
}
251248
}
252249

@@ -258,7 +255,6 @@ function Process-Local-Block {
258255
$log.RotateMainOnly()
259256
}catch {
260257
Write-Error "Failed to rotate log $($log['logfile'].FullName)." -ErrorAction Continue
261-
throw
262258
}
263259
}
264260

@@ -270,7 +266,6 @@ function Process-Local-Block {
270266
Start-Script $postrotate $blockpath -ErrorAction $CallerEA
271267
}catch {
272268
Write-Error "Failed to run shared postrotate script for $blockpath. " -ErrorAction Continue
273-
throw
274269
}
275270
}
276271

@@ -282,7 +277,6 @@ function Process-Local-Block {
282277
$log.PostPostRotate()
283278
}catch {
284279
Write-Error "Failed to rotate log $($log['logfile'].FullName)." -ErrorAction Continue
285-
throw
286280
}
287281
}
288282
}else {
@@ -296,7 +290,7 @@ function Process-Local-Block {
296290
if ( $_.status.rotate -and $postrotate ) { $_.Postrotate() }
297291
if ( ! $postrotate -or ( $postrotate -and $_.status.postrotate ) ) { $_.PostPostRotate() }
298292
}catch {
299-
throw
293+
Write-Error -ErrorRecord $_ -ErrorAction Continue
300294
}
301295
}
302296
}
@@ -309,7 +303,6 @@ function Process-Local-Block {
309303
Start-Script $lastaction $blockpath -ErrorAction $CallerEA
310304
}catch {
311305
Write-Error "Failed to run lastaction script for $blockpath. " -ErrorAction Continue
312-
throw
313306
}
314307
}
315308
}else {

src/Log-Rotate/public/Log-Rotate.Tests.ps1

+42-20
Original file line numberDiff line numberDiff line change
@@ -48,61 +48,61 @@ Describe "Log-Rotate" -Tag 'Unit' {
4848

4949
Context 'Invalid parameters (Non-Terminating)' {
5050

51-
$eaPreference = 'Continue'
51+
$ErrorActionPreference = 'Continue'
5252

5353
It 'errors when config is null' {
5454
$invalidConfig = $null
5555

56-
$err = Log-Rotate -Config $invalidConfig -ErrorAction $eaPreference 2>&1
56+
$err = Log-Rotate -Config $invalidConfig -ErrorVariable err 2>&1
5757
$err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | % { $_.Exception.Message } | Should -Contain "No config file(s) specified."
5858
}
5959

6060
It 'errors when config is an non-existing file' {
6161
$invalidConfig = 'foo'
6262
Mock Test-Path { $false }
6363

64-
$err = Log-Rotate -Config $invalidConfig -ErrorAction $eaPreference 2>&1
64+
$err = Log-Rotate -Config $invalidConfig 2>&1
6565
$err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | % { $_.Exception.Message } | Should -Contain "Invalid config path specified: $invalidConfig"
6666
}
6767

6868
It 'errors when configAsString is null' {
6969
$invalidConfigAsString = $null
7070

71-
$err = Log-Rotate -ConfigAsString $invalidConfigAsString -ErrorAction $eaPreference 2>&1
71+
$err = Log-Rotate -ConfigAsString $invalidConfigAsString 2>&1
7272
$err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | % { $_.Exception.Message } | Should -Contain "No config file(s) specified."
7373
}
7474
}
7575

7676
Context 'Invalid parameters (Terminating)' {
7777

78-
$eaPreference = 'Stop'
78+
$ErrorActionPreference = 'Stop'
7979

8080
It 'errors when config is null' {
8181
$invalidConfig = $null
8282

83-
{ Log-Rotate -Config $invalidConfig -ErrorAction $eaPreference 2>$null } | Should -Throw "No config file(s) specified."
83+
{ Log-Rotate -Config $invalidConfig 2>$null } | Should -Throw "No config file(s) specified."
8484
}
8585

8686
It 'errors when config is an non-existing file' {
8787
$invalidConfig = 'foo'
8888
Mock Test-Path { $false }
8989
6
90-
{ Log-Rotate -Config $invalidConfig -ErrorAction $eaPreference 2>$null } | Should -Throw "Invalid config path specified: $invalidConfig"
90+
{ Log-Rotate -Config $invalidConfig 2>$null } | Should -Throw "Invalid config path specified: $invalidConfig"
9191
}
9292

9393
It 'errors when configAsString is null' {
9494
$invalidConfigAsString = $null
9595

96-
{ Log-Rotate -ConfigAsString $invalidConfigAsString -ErrorAction $eaPreference 2>$null } | Should -Throw "No config file(s) specified."
96+
{ Log-Rotate -ConfigAsString $invalidConfigAsString 2>$null } | Should -Throw "No config file(s) specified."
9797
}
9898
}
9999

100100
Context 'Functionality' {
101101

102-
$eaPreference = 'Stop'
102+
$ErrorActionPreference = 'Stop'
103103

104104
It 'shows the help' {
105-
$help = Log-Rotate -Help -ErrorAction $eaPreference
105+
$help = Log-Rotate -Help
106106

107107
$help | Should -Not -Be $null
108108
}
@@ -111,7 +111,7 @@ Describe "Log-Rotate" -Tag 'Unit' {
111111
. $initScriptBlock
112112
Mock Compile-Full-Config {}
113113

114-
Log-Rotate -config $configFile -ErrorAction $eaPreference
114+
Log-Rotate -config $configFile
115115

116116
Assert-MockCalled Compile-Full-Config -Times 1
117117
}
@@ -121,7 +121,7 @@ Describe "Log-Rotate" -Tag 'Unit' {
121121
Mock Test-Path -ParameterFilter { $Path -eq 'foo' -and $PathType } { $false }
122122
Mock Compile-Full-Config {}
123123

124-
Log-Rotate -config $configFile -ErrorAction $eaPreference
124+
Log-Rotate -config $configFile
125125

126126
Assert-MockCalled Compile-Full-Config -Times 1
127127
}
@@ -130,7 +130,7 @@ Describe "Log-Rotate" -Tag 'Unit' {
130130
. $initScriptBlock
131131
Mock Validate-Full-Config {}
132132

133-
Log-Rotate -config $configFile -ErrorAction $eaPreference
133+
Log-Rotate -config $configFile
134134

135135
Assert-MockCalled Validate-Full-Config -Times 1
136136
}
@@ -151,23 +151,23 @@ Describe "Log-Rotate" -Tag 'Unit' {
151151
$BlockFactory
152152
}
153153

154-
Log-Rotate -config $configFile -ErrorAction $eaPreference
154+
Log-Rotate -config $configFile
155155

156156
Assert-MockCalled New-BlockFactory -Times 1
157157
}
158158

159159
It 'instantiates singleton LogFactory' {
160160
. $initScriptBlock
161161

162-
Log-Rotate -config $configFile -ErrorAction $eaPreference
162+
Log-Rotate -config $configFile
163163

164164
Assert-MockCalled New-LogFactory -Times 1
165165
}
166166

167167
It 'instantiates singleton LogObject' {
168168
. $initScriptBlock
169169

170-
Log-Rotate -config $configFile -ErrorAction $eaPreference
170+
Log-Rotate -config $configFile
171171

172172
Assert-MockCalled New-LogObject -Times 1
173173
}
@@ -190,7 +190,7 @@ Describe "Log-Rotate" -Tag 'Unit' {
190190
$BlockFactory
191191
}
192192

193-
$result = Log-Rotate -config $configFile -ErrorAction $eaPreference
193+
$result = Log-Rotate -config $configFile
194194

195195
$result | Should -Be 'create'
196196
}
@@ -206,15 +206,15 @@ Describe "Log-Rotate" -Tag 'Unit' {
206206
$LogFactory
207207
}
208208

209-
$result = Log-Rotate -config $configFile -ErrorAction $eaPreference
209+
$result = Log-Rotate -config $configFile
210210

211211
$result | Should -Be 'initstatus'
212212
}
213213

214214
It 'processes a block configuration' {
215215
. $initScriptBlock
216216

217-
Log-Rotate -config $configFile -ErrorAction $eaPreference
217+
Log-Rotate -config $configFile
218218

219219
Assert-MockCalled Process-Local-Block -Times 1
220220
}
@@ -231,9 +231,31 @@ Describe "Log-Rotate" -Tag 'Unit' {
231231
$LogFactory
232232
}
233233

234-
$result = Log-Rotate -config $configFile -ErrorAction $eaPreference
234+
$result = Log-Rotate -config $configFile
235235

236236
$result | Should -Be 'dumpstatus'
237237
}
238+
239+
It 'Throws no exception only when specified' {
240+
. $initScriptBlock
241+
Mock Process-Local-Block {
242+
Write-Error 'foo' -ErrorAction Continue
243+
}
244+
245+
# Expect no exception
246+
$err = Log-Rotate -config $configFile -ErrorAction Continue 2>&1
247+
$err | ? { $_ -is [System.Management.Automation.ErrorRecord] } | % { $_.Exception.Message } | Should -Be 'foo'
248+
}
249+
250+
It 'Throws exception only when specified' {
251+
. $initScriptBlock
252+
Mock Process-Local-Block {
253+
Write-Error 'foo' -ErrorAction Stop
254+
}
255+
256+
# Expect exception
257+
{ Log-Rotate -config $configFile -ErrorAction Stop } | Should -Throw 'foo'
258+
}
259+
238260
}
239261
}

0 commit comments

Comments
 (0)