@@ -5,7 +5,6 @@ filter GetConciseMessage {
55 [System.Management.Automation.ErrorRecord ]
66 $InputObject
77 )
8- $err = $InputObject
98 $posmsg = ' '
109 $headerWhitespace = ' '
1110 $message = ' '
@@ -14,44 +13,45 @@ filter GetConciseMessage {
1413 # 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
1514 # 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
1615 # 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' )
2120
2221 # The checks here determine if we show line detailed error information:
2322 # - 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 ]
2625
2726 # - 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
2929
3030 # - 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
3131 $shouldShowLineDetail = ($isParseError -or $isMultiLineOrExternal ) -and
32- $myinv .ScriptName -notmatch ' \.psm1$'
32+ $Invocation .ScriptName -notmatch ' \.psm1$'
3333
3434 if ($useTargetObject -or $shouldShowLineDetail ) {
3535
3636 if ($useTargetObject ) {
37- $posmsg = " ${resetcolor} $ ( $err .TargetObject.File ) ${newline} "
38- } elseif ($myinv .ScriptName ) {
37+ $posmsg = " ${resetcolor} $ ( $InputObject .TargetObject.File ) ${newline} "
38+ } elseif ($Invocation .ScriptName ) {
3939 if ($env: TERM_PROGRAM -eq ' vscode' ) {
4040 # 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} "
4242 } else {
43- $posmsg = " ${resetcolor} $ ( $myinv .ScriptName ) :$ ( $myinv .ScriptLineNumber ) ${newline} "
43+ $posmsg = " ${resetcolor} $ ( $Invocation .ScriptName ) :$ ( $Invocation .ScriptLineNumber ) ${newline} "
4444 }
4545 } else {
4646 $posmsg = " ${newline} "
4747 }
4848
4949 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
5252 } else {
53- $scriptLineNumber = $myinv .ScriptLineNumber
54- $scriptLineNumberLength = $myinv .ScriptLineNumber.ToString ().Length
53+ $scriptLineNumber = $Invocation .ScriptLineNumber
54+ $scriptLineNumberLength = $Invocation .ScriptLineNumber.ToString ().Length
5555 }
5656
5757 if ($scriptLineNumberLength -gt 4 ) {
@@ -72,7 +72,7 @@ filter GetConciseMessage {
7272 $offsetLength = 0
7373 $offsetInLine = 0
7474 } else {
75- $positionMessage = $myinv .PositionMessage.Split ($newline )
75+ $positionMessage = $Invocation .PositionMessage.Split ($newline )
7676 $line = $positionMessage [1 ].Substring(1 ) # skip the '+' at the start
7777 $highlightLine = $positionMessage [$positionMessage.Count - 1 ].Substring(1 )
7878 $offsetLength = $highlightLine.Trim ().Length
@@ -96,23 +96,23 @@ filter GetConciseMessage {
9696 $message = " ${prefix} "
9797 }
9898
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 " )) {
101101 # 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
107107 } else {
108- $message += $err .ToString ()
108+ $message += $InputObject .ToString ()
109109 }
110110 } else {
111- $message += $err .ErrorDetails.Message
111+ $message += $InputObject .ErrorDetails.Message
112112 }
113113
114114 # 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' ) {
116116 $prefixLength = [System.Management.Automation.Internal.StringDecorated ]::new($prefix ).ContentLength
117117 $prefixVtLength = $prefix.Length - $prefixLength
118118
@@ -152,22 +152,22 @@ filter GetConciseMessage {
152152 $posmsg += " ${errorColor} " + $message
153153
154154 $reason = ' Error'
155- if ($err .Exception -and $err .Exception.WasThrownFromThrowStatement ) {
155+ if ($InputObject .Exception -and $InputObject .Exception.WasThrownFromThrowStatement ) {
156156 $reason = ' Exception'
157157 # 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 ) {
161161 # 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
171171 }
172172
173173 " ${errorColor}${reason} : ${posmsg}${resetcolor} "
0 commit comments