-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test-Error.ps1
executable file
·219 lines (198 loc) · 10.2 KB
/
Test-Error.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
param(
[ValidateSet("Test-ErrorCustom","Test-ErrorCustomBroken","Test-ErrorWriteErrorThrowString","Test-ErrorWriteErrorThrowObject","Test-ErrorWriteErrorErrActionStop","Test-ErrorWriteErrorReturn","Test-ErrorWriteErrorReturnTryCatch","Test-ErrorWriteErrorErrActionStopTryCatch","Test-ErrorWriteErrorErrActionStopSimpleFunction","Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion")]
[string]$testFunction,
[ValidateSet("dollarQuestionCapture","tryCatchCapture")]
[string]$testType,
[string]$outputFile
)
function Test-ErrorCustom {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing custom error with return"
1/$divideBy
if (!$?){
$errObj = new-object System.Management.Automation.RuntimeException "Omigosh you tried to divide by 0?"
$category = [System.Management.Automation.ErrorCategory]::NotSpecified
$errRecord = new-object System.Management.Automation.ErrorRecord $errObj, "PathNotFound", $category, $divideBy
$psCmdlet.WriteError($errRecord)
return $errRecord
}
}
function Test-ErrorCustomBroken {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing broken custom error with return"
1/$divideBy
if (!$?){
$errObj = new-object System.Management.Automation.ItemNotFoundException "Omigosh you tried to divide by 0?"
$category = [This.Error.Is.NOT.Right.Because.the.Error.is.About.the.Error]::ObjectNotFound
$errRecord = new-object System.Management.Automation.ErrorRecord $errObj, "PathNotFound", $category, $divideBy
$psCmdlet.WriteError($errRecord)
return $errRecord
}
}
function Test-ErrorWriteErrorThrowString {
# Write-Error with throw
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with throw (string)"
1/$divideBy
if (!$?){
Write-Error "here is my error in my function"
throw "git outta here"
}
}
function Test-ErrorWriteErrorThrowObject {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with throw (object)"
1/$divideBy
if (!$?){
Write-Error "here is my error in my function"
Throw [System.Management.Automation.ValidationMetadataException] "I returned an error and I'm so proud."
}
}
function Test-ErrorWriteErrorReturn {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with ErrorAction Stop using Try/Catch"
1/$divideBy
if (!$?){
Write-error "here is my error in my function using dollarquestion and return for stdout"
return "Hey buddy you had an error that you should check out"
}
write-host "We got past the error" -foregroundcolor green
}
function Test-ErrorWriteErrorReturnTryCatch {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with ErrorAction Stop using Try/Catch"
try {1/$divideBy}
catch {
Write-error "here is my error in my function using try/catch and return for stdout"
return "Hey buddy you had an error that you should check out"
}
write-host "We got past the error" -foregroundcolor green
}
function Test-ErrorWriteErrorErrActionStop {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with ErrorAction Stop"
1/$divideBy
if (!$?){Write-Error "here is my error in my function" -ErrorAction Stop}
}
function Test-ErrorWriteErrorErrActionStopTryCatch {
[CmdletBinding()]
Param([Parameter(Position=0,mandatory=$false)][int]$divideBy=0)
Write-Host "Executing Write-Error with ErrorAction Stop using Try/Catch"
try {1/$divideBy}
catch {Write-error "here is my error in my function using try/catch" -ErrorAction stop}
write-host "We got past the error" -foregroundcolor green
}
function Test-ErrorWriteErrorErrActionStopSimpleFunction {
Write-Host "Executing non-advanced function with try/catch and terminating write-error/erroraction stop"
try {1/0}
catch {Write-error "here is my error in my simple, similar behaving function" -ErrorAction stop}
write-host "We got past the error" -foregroundcolor green
}
function Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion {
Write-Host "Executing non-advanced function with try/catch, error[0] conversion to string, and terminating write-error/erroraction stop"
try {1/0}
catch {Write-error "That really didn't work. System Error:`r`n`t$($Error[0] | Out-String)" -ErrorAction stop}
write-host "We got past the error" -foregroundcolor green
}
function Set-OutputTrackerObject {
Param(
[Parameter(Position=0,mandatory=$true)]$testFunction,
[Parameter(Position=1,mandatory=$true)]$testType,
[Parameter(Position=2,mandatory=$true)]$trapStatus,
[Parameter(Position=3,mandatory=$true)]$standardOut,
[Parameter(Position=4,mandatory=$true)]$errorStream
)
$trackerObject=@([pscustomobject]@{entry=$testFunction;type=$testType;successfulTrap=$trapStatus;standardOutput=$standardOut;errorStream=$errorStream})
return $trackerObject
}
function Write-OutputTrackerObject {
Param(
[Parameter(Position=0,mandatory=$true)]$objectToWrite,
[Parameter(Position=1,mandatory=$true)]$fileToWriteTo
)
$objectToWrite| Export-Clixml $fileToWriteTo -Force
if (!$?){return $false}
else {return $true}
}
$Error.Clear()
if ($output){Remove-Variable output}
$successfultrap=$false
$standardOut="null"
$errorStream=$Error.Clone()
$outputTrackerDataObject=Set-OutputTrackerObject $testFunction $testType $successfultrap $standardOut $errorStream
$writeStatus=Write-OutputTrackerObject $outputTrackerDataObject $PSScriptRoot\object$random.xml
if (!$writeStatus){throw "Could not persist object to file"}
Write-Warning "*******************************TESTING FUNCTION $testFunction using $testType ********************************** `n"
switch ($testType){
dollarQuestionCapture{
#NOTE: We can't do Invoke-Expression $testFunction because $? will always be true since invoke-expression actually succeeded.
switch ($testFunction){
Test-ErrorCustom{$output=Test-ErrorCustom}
Test-ErrorCustomBroken{$output=Test-ErrorCustomBroken}
Test-ErrorWriteErrorThrowString{$output=Test-ErrorWriteErrorThrowString}
Test-ErrorWriteErrorThrowObject{$output=Test-ErrorWriteErrorThrowObject}
Test-ErrorWriteErrorErrActionStop{$output=Test-ErrorWriteErrorErrActionStop}
Test-ErrorWriteErrorErrActionStopTryCatch{$output=Test-ErrorWriteErrorErrActionStopTryCatch}
Test-ErrorWriteErrorErrActionStopSimpleFunction{$output=Test-ErrorWriteErrorErrActionStopSimpleFunction}
Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion{$output=Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion}
}
if (!$?){
$successfultrap=$true
if ($output -eq $null){$standardOut="null"}
else {$standardOut=$output}
$errorStream=$Error.Clone()
$outputTrackerDataObject=Set-OutputTrackerObject $testFunction $testType $successfultrap $standardOut $errorStream
$writeStatus=Write-OutputTrackerObject $outputTrackerDataObject $outputFile
if (!$writeStatus){throw "Could not persist object to file"}
}
else {
$successfultrap=$false
if ($output -eq $null){$standardOut="null"}
else {$standardOut=$output}
$errorStream=$Error.Clone()
$outputTrackerDataObject=Set-OutputTrackerObject $testFunction $testType $successfultrap $standardOut $errorStream
$writeStatus=Write-OutputTrackerObject $outputTrackerDataObject $outputFile
if (!$writeStatus){throw "Could not persist object to file"}
}
}
tryCatchCapture{
try{
switch ($testFunction){
Test-ErrorCustom{$output=Test-ErrorCustom}
Test-ErrorCustomBroken{$output=Test-ErrorCustomBroken}
Test-ErrorWriteErrorThrowString{$output=Test-ErrorWriteErrorThrowString}
Test-ErrorWriteErrorThrowObject{$output=Test-ErrorWriteErrorThrowObject}
Test-ErrorWriteErrorErrActionStop{$output=Test-ErrorWriteErrorErrActionStop}
Test-ErrorWriteErrorErrActionStopTryCatch{$output=Test-ErrorWriteErrorErrActionStopTryCatch}
Test-ErrorWriteErrorErrActionStopSimpleFunction{$output=Test-ErrorWriteErrorErrActionStopSimpleFunction}
Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion{$output=Test-ErrorWriteErrorErrActionStopSimpleFunctionWithERRStringConversion}
}
}
catch {
$successfultrap=$true
if ($output -eq $null){$standardOut="null"}
else {$standardOut=$output}
$errorStream=$Error.Clone()
$outputTrackerDataObject=Set-OutputTrackerObject $testFunction $testType $successfultrap $standardOut $errorStream
$writeStatus=Write-OutputTrackerObject $outputTrackerDataObject $outputFile
if (!$writeStatus){throw "Could not persist object to file"}
}
if (!$successfultrap){
$successfultrap=$false
if ($output -eq $null){$standardOut="null"}
else {$standardOut=$output}
$errorStream=$Error.Clone()
$outputTrackerDataObject=Set-OutputTrackerObject $testFunction $testType $successfultrap $standardOut $errorStream
$writeStatus=Write-OutputTrackerObject $outputTrackerDataObject $outputFile
if (!$writeStatus){throw "Could not persist object to file"}
}
}
}
Write-Host "******************************* $testFunction TESTING COMPLETE using $testType **********************************`n`n" -ForegroundColor Black -BackgroundColor White