Skip to content

Commit f84e6e5

Browse files
authored
Merge pull request #1554 from SteveL-MSFT/haderrors
Change PSScript resource to not exit on non-terminating errors
2 parents 6c162ed + 62d3be2 commit f84e6e5

2 files changed

Lines changed: 76 additions & 21 deletions

File tree

resources/PSScript/psscript.ps1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,40 @@ try {
138138
$asyncResult = $ps.BeginInvoke()
139139
while (-not $asyncResult.IsCompleted) {
140140
Write-TraceQueue
141-
141+
142142
Start-Sleep -Milliseconds 100
143143
}
144+
145+
if ($ps.InvocationStateInfo.State -eq 'Failed') {
146+
$record = $ps.InvocationStateInfo.Reason.ErrorRecord
147+
$message = if ($null -ne $record) {
148+
"Script failed with terminating error at line {0} for statement ``{1}`` - {2}" -f @(
149+
$record.InvocationInfo.ScriptLineNumber,
150+
$record.InvocationInfo.Line,
151+
$record.Exception.ToString()
152+
)
153+
} else {
154+
"Script failed with terminating error: $($ps.InvocationStateInfo.Reason)"
155+
}
156+
Write-DscTrace -Now -Level Error -Message $message
157+
exit 1
158+
}
159+
144160
$outputCollection = $ps.EndInvoke($asyncResult)
145161
Write-TraceQueue
146162

147163

148164
if ($ps.HadErrors) {
149-
# If there are any errors, we will exit with an error code
150-
Write-DscTrace -Now -Level Error -Message 'Errors occurred during script execution.'
151-
exit 1
165+
# Errors can be non-terminating, so we just write a warning and continue
166+
Write-DscTrace -Now -Level Debug -Message 'Non-terminating errors occurred during script execution.'
152167
}
153168

154169
foreach ($output in $outputCollection) {
155170
$outputObjects.Add($output)
156171
}
157172
}
158173
catch {
159-
Write-DscTrace -Now -Level Error -Message $_
174+
Write-DscTrace -Now -Level Error -Message ($_ | Format-List -Force * | Out-String)
160175
exit 1
161176
}
162177
finally {

resources/PSScript/psscript.tests.ps1

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ Describe 'Tests for PSScript resource' {
108108
$result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
109109
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
110110
$result | Should -BeNullOrEmpty -Because "Test operation should return an error"
111-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*Test operation did not return a single boolean value.*'
111+
$errorLog = Get-Content $TestDrive/error.txt -Raw
112+
$errorLog | Should -BeLike '*ERROR*:*Test operation did not return a single boolean value.*' -Because $errorLog
112113
}
113114

114115
It 'Test operation returns error for multiple boolean results for <resourceType>' -TestCases $testCases {
@@ -122,7 +123,8 @@ Describe 'Tests for PSScript resource' {
122123
$result = dsc resource test -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
123124
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
124125
$result | Should -BeNullOrEmpty -Because "Test operation should return an error"
125-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*Test operation did not return a single boolean value.*'
126+
$errorLog = Get-Content $TestDrive/error.txt -Raw
127+
$errorLog | Should -BeLike '*ERROR*:*Test operation did not return a single boolean value.*' -Because $errorLog
126128
}
127129

128130
It 'Empty SetScript is ignored for <resourceType>' -TestCases $testCases {
@@ -187,8 +189,9 @@ Describe 'Tests for PSScript resource' {
187189
$result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
188190
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
189191
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
190-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is a warning*'
191-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is second warning*'
192+
$errorLog = Get-Content $TestDrive/error.txt -Raw
193+
$errorLog | Should -BeLike '*WARN*:*This is a warning*' -Because $errorLog
194+
$errorLog | Should -BeLike '*WARN*:*This is second warning*' -Because $errorLog
192195
}
193196

194197
It 'Write-Error shows up as error traces for <resourceType>' -TestCases $testCases {
@@ -201,8 +204,9 @@ Describe 'Tests for PSScript resource' {
201204

202205
$result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
203206
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
207+
$errorLog = Get-Content $TestDrive/error.txt -Raw
204208
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
205-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*ERROR*:*This is an error*'
209+
$errorLog | Should -BeLike '*ERROR*:*This is an error*' -Because $errorLog
206210
}
207211

208212
It 'Write-Verbose shows up as info traces for <resourceType>' -TestCases $testCases {
@@ -214,8 +218,9 @@ Describe 'Tests for PSScript resource' {
214218
'@
215219
$result = dsc -l info resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
216220
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
221+
$errorLog = Get-Content $TestDrive/error.txt -Raw
217222
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
218-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*INFO*:*This is a verbose message*'
223+
$errorLog | Should -BeLike '*INFO*:*This is a verbose message*' -Because $errorLog
219224
}
220225

221226
It 'Write-Debug shows up as debug traces for <resourceType>' -TestCases $testCases {
@@ -227,8 +232,9 @@ Describe 'Tests for PSScript resource' {
227232
'@
228233
$result = dsc -l debug resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
229234
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
235+
$errorLog = Get-Content $TestDrive/error.txt -Raw
230236
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
231-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*DEBUG*:*This is a debug message*'
237+
$errorLog | Should -BeLike '*DEBUG*:*This is a debug message*' -Because $errorLog
232238
}
233239

234240
It 'Write-Information shows up as trace traces for <resourceType>' -TestCases $testCases {
@@ -241,8 +247,9 @@ Describe 'Tests for PSScript resource' {
241247
'@
242248
$result = dsc -l trace resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
243249
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
250+
$errorLog = Get-Content $TestDrive/error.txt -Raw
244251
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
245-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*TRACE*:*This is an information message*'
252+
$errorLog | Should -BeLike '*TRACE*:*This is an information message*' -Because $errorLog
246253
}
247254

248255
It 'A thrown exception results in an error for <resourceType>' -TestCases $testCases {
@@ -254,8 +261,9 @@ Describe 'Tests for PSScript resource' {
254261
'@
255262
$result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
256263
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
264+
$errorLog = Get-Content $TestDrive/error.txt -Raw
257265
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
258-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*ERROR*:*This is an exception*'
266+
$errorLog | Should -BeLike '*ERROR*:*This is an exception*' -Because $errorLog
259267
}
260268

261269
It 'Sample config works' {
@@ -291,8 +299,9 @@ Describe 'Tests for PSScript resource' {
291299
input: "This is a string"
292300
'@
293301
dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
294-
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
295-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*ERROR*:*Input was provided but script does not have a parameter to accept input.*'
302+
$errorLog = Get-Content $TestDrive/error.txt -Raw
303+
$LASTEXITCODE | Should -Be 2 -Because $errorLog
304+
$errorLog | Should -BeLike '*ERROR*:*Input was provided but script does not have a parameter to accept input.*' -Because $errorLog
296305
}
297306

298307
It 'Param without input is an error for <resourceType>' -TestCases $testCases {
@@ -304,8 +313,9 @@ Describe 'Tests for PSScript resource' {
304313
"This should fail"
305314
'@
306315
dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
307-
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
308-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike "*ERROR*:*Script has a parameter 'inputObj' but no input was provided.*"
316+
$errorLog = Get-Content $TestDrive/error.txt -Raw
317+
$LASTEXITCODE | Should -Be 2 -Because $errorLog
318+
$errorLog | Should -BeLike "*ERROR*:*Script has a parameter 'inputObj' but no input was provided.*" -Because $errorLog
309319
}
310320

311321
It 'Write-Host results in an info message for <resourceType>' -TestCases $testCases {
@@ -319,9 +329,39 @@ Describe 'Tests for PSScript resource' {
319329
'@
320330
$result = dsc -l trace resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
321331
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
332+
$errorLog = Get-Content $TestDrive/error.txt -Raw
333+
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
334+
$errorLog | Should -BeLike '*WARN*:*This is a warning*' -Because $errorLog
335+
$errorLog | Should -BeLike '*INFO*:*This is a host message*' -Because $errorLog
336+
$errorLog | Should -BeLike '*INFO*:*This is a verbose message*' -Because $errorLog
337+
}
338+
339+
It 'Non-terminating errors do not cause script to fail' -TestCases $testCases {
340+
param($resourceType)
341+
342+
$yaml = @'
343+
getScript: |
344+
Get-Item "ThisFileDoesNotExist.txt" -ErrorAction SilentlyContinue
345+
"This should still be output"
346+
'@
347+
$result = dsc -l debug resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
348+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
349+
$result.actualState.output.Count | Should -Be 1 -Because ($result | ConvertTo-Json -Depth 10 | Out-String)
350+
$result.actualState.output[0] | Should -BeExactly "This should still be output"
351+
$errorLog = Get-Content $TestDrive/error.txt -Raw
352+
$errorLog | Should -BeLike '*DEBUG*:*Non-terminating errors occurred during script execution.*' -Because $errorLog
353+
}
354+
355+
It 'Terminating errors result in an error for <resourceType>' -TestCases $testCases {
356+
param($resourceType)
357+
358+
$yaml = @'
359+
GetScript: |
360+
Get-Item "ThisFileDoesNotExist.txt" -ErrorAction Stop
361+
'@
362+
$result = dsc resource get -r $resourceType -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
363+
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
322364
$result.actualState.output.Count | Should -Be 0 -Because ($result | ConvertTo-Json | Out-String)
323-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*This is a warning*'
324-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*INFO*:*This is a host message*'
325-
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*INFO*:*This is a verbose message*'
365+
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*ERROR*:*Cannot find path*'
326366
}
327367
}

0 commit comments

Comments
 (0)