@@ -5,7 +5,6 @@ filter GetConciseMessage {
5
5
[System.Management.Automation.ErrorRecord ]
6
6
$InputObject
7
7
)
8
- $err = $InputObject
9
8
$posmsg = ' '
10
9
$headerWhitespace = ' '
11
10
$message = ' '
@@ -14,44 +13,45 @@ filter GetConciseMessage {
14
13
# Handle case where there is a TargetObject from a Pester `Should` assertion failure and we can show the error at the target rather than the script source
15
14
# Note that in some versions, this is a Dictionary<,> and in others it's a hashtable. So we explicitly cast to a shared interface in the method invocation
16
15
# to force using `IDictionary.Contains`. Hashtable does have it's own `ContainKeys` as well, but if they ever opt to use a custom `IDictionary`, that may not.
17
- $useTargetObject = $null -ne $err .TargetObject -and
18
- $err .TargetObject -is [System.Collections.IDictionary ] -and
19
- ([System.Collections.IDictionary ]$err .TargetObject ).Contains(' Line' ) -and
20
- ([System.Collections.IDictionary ]$err .TargetObject ).Contains(' LineText' )
16
+ $useTargetObject = $null -ne $InputObject .TargetObject -and
17
+ $InputObject .TargetObject -is [System.Collections.IDictionary ] -and
18
+ ([System.Collections.IDictionary ]$InputObject .TargetObject ).Contains(' Line' ) -and
19
+ ([System.Collections.IDictionary ]$InputObject .TargetObject ).Contains(' LineText' )
21
20
22
21
# The checks here determine if we show line detailed error information:
23
22
# - check if `ParserError` and comes from PowerShell which eventually results in a ParseException, but during this execution it's an ErrorRecord
24
- $isParseError = $err .CategoryInfo.Category -eq ' ParserError' -and
25
- $err .Exception -is [System.Management.Automation.ParentContainsErrorRecordException ]
23
+ $isParseError = $InputObject .CategoryInfo.Category -eq ' ParserError' -and
24
+ $InputObject .Exception -is [System.Management.Automation.ParentContainsErrorRecordException ]
26
25
27
26
# - check if invocation is a script or multiple lines in the console
28
- $isMultiLineOrExternal = $myinv.ScriptName -or $myinv.ScriptLineNumber -gt 1
27
+ $Invocation = $InputObject.InvocationInfo
28
+ $isMultiLineOrExternal = $Invocation.ScriptName -or $Invocation.ScriptLineNumber -gt 1
29
29
30
30
# - check that it's not a script module as expectation is that users don't want to see the line of error within a module
31
31
$shouldShowLineDetail = ($isParseError -or $isMultiLineOrExternal ) -and
32
- $myinv .ScriptName -notmatch ' \.psm1$'
32
+ $Invocation .ScriptName -notmatch ' \.psm1$'
33
33
34
34
if ($useTargetObject -or $shouldShowLineDetail ) {
35
35
36
36
if ($useTargetObject ) {
37
- $posmsg = " ${resetcolor} $ ( $err .TargetObject.File ) ${newline} "
38
- } elseif ($myinv .ScriptName ) {
37
+ $posmsg = " ${resetcolor} $ ( $InputObject .TargetObject.File ) ${newline} "
38
+ } elseif ($Invocation .ScriptName ) {
39
39
if ($env: TERM_PROGRAM -eq ' vscode' ) {
40
40
# If we are running in vscode, we know the file:line:col links are clickable so we use this format
41
- $posmsg = " ${resetcolor} $ ( $myinv .ScriptName ) :$ ( $myinv .ScriptLineNumber ) :$ ( $myinv .OffsetInLine ) ${newline} "
41
+ $posmsg = " ${resetcolor} $ ( $Invocation .ScriptName ) :$ ( $Invocation .ScriptLineNumber ) :$ ( $Invocation .OffsetInLine ) ${newline} "
42
42
} else {
43
- $posmsg = " ${resetcolor} $ ( $myinv .ScriptName ) :$ ( $myinv .ScriptLineNumber ) ${newline} "
43
+ $posmsg = " ${resetcolor} $ ( $Invocation .ScriptName ) :$ ( $Invocation .ScriptLineNumber ) ${newline} "
44
44
}
45
45
} else {
46
46
$posmsg = " ${newline} "
47
47
}
48
48
49
49
if ($useTargetObject ) {
50
- $scriptLineNumber = $err .TargetObject.Line
51
- $scriptLineNumberLength = $err .TargetObject.Line.ToString ().Length
50
+ $scriptLineNumber = $InputObject .TargetObject.Line
51
+ $scriptLineNumberLength = $InputObject .TargetObject.Line.ToString ().Length
52
52
} else {
53
- $scriptLineNumber = $myinv .ScriptLineNumber
54
- $scriptLineNumberLength = $myinv .ScriptLineNumber.ToString ().Length
53
+ $scriptLineNumber = $Invocation .ScriptLineNumber
54
+ $scriptLineNumberLength = $Invocation .ScriptLineNumber.ToString ().Length
55
55
}
56
56
57
57
if ($scriptLineNumberLength -gt 4 ) {
@@ -72,7 +72,7 @@ filter GetConciseMessage {
72
72
$offsetLength = 0
73
73
$offsetInLine = 0
74
74
} else {
75
- $positionMessage = $myinv .PositionMessage.Split ($newline )
75
+ $positionMessage = $Invocation .PositionMessage.Split ($newline )
76
76
$line = $positionMessage [1 ].Substring(1 ) # skip the '+' at the start
77
77
$highlightLine = $positionMessage [$positionMessage.Count - 1 ].Substring(1 )
78
78
$offsetLength = $highlightLine.Trim ().Length
@@ -96,23 +96,23 @@ filter GetConciseMessage {
96
96
$message = " ${prefix} "
97
97
}
98
98
99
- if (! $err .ErrorDetails -or ! $err .ErrorDetails.Message ) {
100
- if ($err .CategoryInfo.Category -eq ' ParserError' -and $err .Exception.Message.Contains (" ~$newline " )) {
99
+ if (! $InputObject .ErrorDetails -or ! $InputObject .ErrorDetails.Message ) {
100
+ if ($InputObject .CategoryInfo.Category -eq ' ParserError' -and $InputObject .Exception.Message.Contains (" ~$newline " )) {
101
101
# need to parse out the relevant part of the pre-rendered positionmessage
102
- $message += $err .Exception.Message.split (" ~$newline " )[1 ].split(" ${newline}${newline} " )[0 ]
103
- } elseif ($err .Exception ) {
104
- $message += $err .Exception.Message
105
- } elseif ($err .Message ) {
106
- $message += $err .Message
102
+ $message += $InputObject .Exception.Message.split (" ~$newline " )[1 ].split(" ${newline}${newline} " )[0 ]
103
+ } elseif ($InputObject .Exception ) {
104
+ $message += $InputObject .Exception.Message
105
+ } elseif ($InputObject .Message ) {
106
+ $message += $InputObject .Message
107
107
} else {
108
- $message += $err .ToString ()
108
+ $message += $InputObject .ToString ()
109
109
}
110
110
} else {
111
- $message += $err .ErrorDetails.Message
111
+ $message += $InputObject .ErrorDetails.Message
112
112
}
113
113
114
114
# if rendering line information, break up the message if it's wider than the console
115
- if ($myinv -and $myinv .ScriptName -or $err .CategoryInfo.Category -eq ' ParserError' ) {
115
+ if ($Invocation -and $Invocation .ScriptName -or $InputObject .CategoryInfo.Category -eq ' ParserError' ) {
116
116
$prefixLength = [System.Management.Automation.Internal.StringDecorated ]::new($prefix ).ContentLength
117
117
$prefixVtLength = $prefix.Length - $prefixLength
118
118
@@ -152,22 +152,22 @@ filter GetConciseMessage {
152
152
$posmsg += " ${errorColor} " + $message
153
153
154
154
$reason = ' Error'
155
- if ($err .Exception -and $err .Exception.WasThrownFromThrowStatement ) {
155
+ if ($InputObject .Exception -and $InputObject .Exception.WasThrownFromThrowStatement ) {
156
156
$reason = ' Exception'
157
157
# MyCommand can be the script block, so we don't want to show that so check if it's an actual command
158
- } elseif ($myinv .MyCommand -and $myinv .MyCommand.Name -and (Get-Command - Name $myinv .MyCommand - ErrorAction Ignore)) {
159
- $reason = $myinv .MyCommand
160
- } elseif ($err .CategoryInfo.Activity ) {
158
+ } elseif ($Invocation .MyCommand -and $Invocation .MyCommand.Name -and (Get-Command - Name $Invocation .MyCommand - ErrorAction Ignore)) {
159
+ $reason = $Invocation .MyCommand
160
+ } elseif ($InputObject .CategoryInfo.Activity ) {
161
161
# If it's a scriptblock, better to show the command in the scriptblock that had the error
162
- $reason = $err .CategoryInfo.Activity
163
- } elseif ($myinv .MyCommand ) {
164
- $reason = $myinv .MyCommand
165
- } elseif ($myinv .InvocationName ) {
166
- $reason = $myinv .InvocationName
167
- } elseif ($err .CategoryInfo.Category ) {
168
- $reason = $err .CategoryInfo.Category
169
- } elseif ($err .CategoryInfo.Reason ) {
170
- $reason = $err .CategoryInfo.Reason
162
+ $reason = $InputObject .CategoryInfo.Activity
163
+ } elseif ($Invocation .MyCommand ) {
164
+ $reason = $Invocation .MyCommand
165
+ } elseif ($Invocation .InvocationName ) {
166
+ $reason = $Invocation .InvocationName
167
+ } elseif ($InputObject .CategoryInfo.Category ) {
168
+ $reason = $InputObject .CategoryInfo.Category
169
+ } elseif ($InputObject .CategoryInfo.Reason ) {
170
+ $reason = $InputObject .CategoryInfo.Reason
171
171
}
172
172
173
173
" ${errorColor}${reason} : ${posmsg}${resetcolor} "
0 commit comments