Skip to content

Commit b9ced29

Browse files
committed
Clean up a Simple ErrorView
1 parent 815e19a commit b9ced29

File tree

3 files changed

+88
-50
lines changed

3 files changed

+88
-50
lines changed

ErrorView.ps1xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
<CustomEntry>
1313
<CustomItem>
1414
<ExpressionBinding>
15-
<ScriptBlock>
16-
Write-NativeCommandError $_
17-
</ScriptBlock>
15+
<ScriptBlock>Write-NativeCommandError $_</ScriptBlock>
1816
</ExpressionBinding>
1917
<ExpressionBinding>
2018
<ScriptBlock>
21-
if ($formatter = Get-Command "ConvertTo-Error*$($ErrorView)*" -ListImported) {
22-
&amp;@($formatter)[0] $_
23-
} else {
24-
ConvertTo-ErrorNormalView $_
25-
}
19+
<![CDATA[
20+
if ($formatter = Get-Command ConvertTo-$($global:ErrorView -replace "View")ErrorView -ListImported -ErrorAction Ignore -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord]) {
21+
&@($formatter)[0] $_
22+
} else {
23+
ConvertTo-NormalErrorView $_
24+
}
25+
]]>
2626
</ScriptBlock>
2727
</ExpressionBinding>
2828
</CustomItem>

ErrorView.psd1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
Copyright = '(c) Joel Bennett. All rights reserved.'
1212

13-
FunctionsToExport = 'Write-NativeCommandError', 'ConvertTo-ErrorCategoryView', 'ConvertTo-ErrorNormalView', 'ConvertTo-ErrorSimpleView'
13+
FunctionsToExport = 'Format-Error', 'Write-NativeCommandError', 'ConvertTo-CategoryErrorView', 'ConvertTo-NormalErrorView', 'ConvertTo-SimpleErrorView'
1414

15-
CmdletsToExport = @()
16-
VariablesToExport = @()
17-
AliasesToExport = @()
15+
CmdletsToExport = @()
16+
VariablesToExport = @()
17+
AliasesToExport = @("fe")
1818

1919
PrivateData = @{
2020
PSData = @{

ErrorView.psm1

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
param(
2-
[ArgumentCompleter({ (Get-Command ConvertTo-Error* -ListImported).Name -replace "ConvertTo-Error(?:View)?(.*)(?:View)",'$1' })]
3-
$ErrorView
2+
$global:ErrorView = "Simple"
43
)
54

65
# We need to overwrite the ErrorView
76
# So -PrependPath, instead of FormatsToProcess
87
Update-FormatData -PrependPath $PSScriptRoot\ErrorView.ps1xml
98

9+
function Format-Error {
10+
[CmdletBinding()]
11+
[Alias("fe")]
12+
[OutputType([System.Management.Automation.ErrorRecord])]
13+
param(
14+
[Parameter(Position=0, ValueFromPipelineByPropertyName)]
15+
[ArgumentCompleter({
16+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
17+
[System.Management.Automation.CompletionResult[]]((
18+
Get-Command ConvertTo-*ErrorView -ListImported -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord]
19+
).Name -replace "ConvertTo-(.*)ErrorView",'$1' -like "*$($wordToComplete)*")
20+
})]
21+
$View = $ErrorView,
22+
23+
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
24+
[System.Management.Automation.ErrorRecord]$InputObject
25+
26+
)
27+
begin {
28+
$View, $global:ErrorView = $ErrorView, $View
29+
}
30+
process {
31+
$InputObject
32+
}
33+
end {
34+
$global:ErrorView = $View
35+
}
36+
}
37+
1038
function Write-NativeCommandError {
1139
[CmdletBinding()]
1240
param(
1341
[System.Management.Automation.ErrorRecord]
14-
$CurrentError
42+
$InputObject
1543
)
1644

17-
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") { return }
45+
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") { return }
1846

19-
$myinv = $CurrentError.InvocationInfo
47+
$myinv = $InputObject.InvocationInfo
2048
if ($myinv -and $myinv.MyCommand) {
2149
switch -regex ( $myinv.MyCommand.CommandType ) {
2250
([System.Management.Automation.CommandTypes]::ExternalScript) {
@@ -46,29 +74,38 @@ function Write-NativeCommandError {
4674
$myinv.InvocationName + " : "
4775
}
4876
}
49-
function ConvertTo-ErrorCategoryView {
77+
78+
function ConvertTo-CategoryErrorView {
5079
[CmdletBinding()]
5180
param(
5281
[System.Management.Automation.ErrorRecord]
53-
$CurrentError
82+
$InputObject
5483
)
5584

56-
$CurrentError.CategoryInfo.GetMessage()
85+
$InputObject.CategoryInfo.GetMessage()
5786
}
5887

59-
function ConvertTo-ErrorSimpleView {
88+
function ConvertTo-SimpleErrorView {
6089
[CmdletBinding()]
6190
param(
6291
[System.Management.Automation.ErrorRecord]
63-
$CurrentError
92+
$InputObject
6493
)
6594

66-
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
67-
$CurrentError.Exception.Message
95+
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
96+
$InputObject.Exception.Message
6897
} else {
69-
$myinv = $CurrentError.InvocationInfo
70-
if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne 'ParserError'))) {
71-
$posmsg = $myinv.PositionMessage
98+
$myinv = $InputObject.InvocationInfo
99+
if ($myinv -and ($myinv.MyCommand -or ($InputObject.CategoryInfo.Category -ne 'ParserError'))) {
100+
# rip off lines that say "At line:1 char:1" (hopefully, in a language agnostic way)
101+
$posmsg = $myinv.PositionMessage -replace "^At line:1 .*[\r\n]+"
102+
# rip off the underline and instead, put >>>markers<<< around the important bit
103+
# we could, instead, set the background to a highlight color?
104+
$pattern = $posmsg -split "[\r\n]+" -match "\+( +~+)\s*" -replace '(~+)', '($1)' -replace '( +)','($1)' -replace '~| ','.'
105+
$posmsg = $posmsg -replace '[\r\n]+\+ +~+'
106+
if ($pattern) {
107+
$posmsg = $posmsg -replace "\+$pattern", '+ $1>>>$2<<<'
108+
}
72109
} else {
73110
$posmsg = ""
74111
}
@@ -77,14 +114,14 @@ function ConvertTo-ErrorSimpleView {
77114
$posmsg = "`n" + $posmsg
78115
}
79116

80-
if ( & { Set-StrictMode -Version 1; $CurrentError.PSMessageDetails } ) {
81-
$posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
117+
if ( & { Set-StrictMode -Version 1; $InputObject.PSMessageDetails } ) {
118+
$posmsg = " : " + $InputObject.PSMessageDetails + $posmsg
82119
}
83120

84121
$indent = 4
85122
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
86123

87-
$originInfo = & { Set-StrictMode -Version 1; $CurrentError.OriginInfo }
124+
$originInfo = & { Set-StrictMode -Version 1; $InputObject.OriginInfo }
88125
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) {
89126
$indentString = "+ PSComputerName : " + $originInfo.PSComputerName
90127
$posmsg += "`n"
@@ -95,25 +132,26 @@ function ConvertTo-ErrorSimpleView {
95132
}
96133
}
97134

98-
if (!$CurrentError.ErrorDetails -or !$CurrentError.ErrorDetails.Message) {
99-
$CurrentError.Exception.Message + $posmsg + "`n "
135+
if (!$InputObject.ErrorDetails -or !$InputObject.ErrorDetails.Message) {
136+
$InputObject.Exception.Message + $posmsg + "`n "
100137
} else {
101-
$CurrentError.ErrorDetails.Message + $posmsg
138+
$InputObject.ErrorDetails.Message + $posmsg
102139
}
103140
}
104141
}
105-
function ConvertTo-ErrorNormalView {
142+
143+
function ConvertTo-NormalErrorView {
106144
[CmdletBinding()]
107145
param(
108146
[System.Management.Automation.ErrorRecord]
109-
$CurrentError
147+
$InputObject
110148
)
111149

112-
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
113-
$CurrentError.Exception.Message
150+
if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
151+
$InputObject.Exception.Message
114152
} else {
115-
$myinv = $CurrentError.InvocationInfo
116-
if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne 'ParserError'))) {
153+
$myinv = $InputObject.InvocationInfo
154+
if ($myinv -and ($myinv.MyCommand -or ($InputObject.CategoryInfo.Category -ne 'ParserError'))) {
117155
$posmsg = $myinv.PositionMessage
118156
} else {
119157
$posmsg = ""
@@ -123,18 +161,18 @@ function ConvertTo-ErrorNormalView {
123161
$posmsg = "`n" + $posmsg
124162
}
125163

126-
if ( &{ Set-StrictMode -Version 1; $CurrentError.PSMessageDetails } ) {
127-
$posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
164+
if ( &{ Set-StrictMode -Version 1; $InputObject.PSMessageDetails } ) {
165+
$posmsg = " : " + $InputObject.PSMessageDetails + $posmsg
128166
}
129167

130168
$indent = 4
131169
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
132170

133-
$errorCategoryMsg = &{ Set-StrictMode -Version 1; $CurrentError.ErrorCategory_Message }
171+
$errorCategoryMsg = &{ Set-StrictMode -Version 1; $InputObject.ErrorCategory_Message }
134172
if ($null -ne $errorCategoryMsg) {
135-
$indentString = "+ CategoryInfo : " + $CurrentError.ErrorCategory_Message
173+
$indentString = "+ CategoryInfo : " + $InputObject.ErrorCategory_Message
136174
} else {
137-
$indentString = "+ CategoryInfo : " + $CurrentError.CategoryInfo
175+
$indentString = "+ CategoryInfo : " + $InputObject.CategoryInfo
138176
}
139177
$posmsg += "`n"
140178
foreach ($line in @($indentString -split "(.{$width})")) {
@@ -143,15 +181,15 @@ function ConvertTo-ErrorNormalView {
143181
}
144182
}
145183

146-
$indentString = "+ FullyQualifiedErrorId : " + $CurrentError.FullyQualifiedErrorId
184+
$indentString = "+ FullyQualifiedErrorId : " + $InputObject.FullyQualifiedErrorId
147185
$posmsg += "`n"
148186
foreach ($line in @($indentString -split "(.{$width})")) {
149187
if ($line) {
150188
$posmsg += (" " * $indent + $line)
151189
}
152190
}
153191

154-
$originInfo = &{ Set-StrictMode -Version 1; $CurrentError.OriginInfo }
192+
$originInfo = &{ Set-StrictMode -Version 1; $InputObject.OriginInfo }
155193
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) {
156194
$indentString = "+ PSComputerName : " + $originInfo.PSComputerName
157195
$posmsg += "`n"
@@ -162,10 +200,10 @@ function ConvertTo-ErrorNormalView {
162200
}
163201
}
164202

165-
if (!$CurrentError.ErrorDetails -or !$CurrentError.ErrorDetails.Message) {
166-
$CurrentError.Exception.Message + $posmsg + "`n "
203+
if (!$InputObject.ErrorDetails -or !$InputObject.ErrorDetails.Message) {
204+
$InputObject.Exception.Message + $posmsg + "`n "
167205
} else {
168-
$CurrentError.ErrorDetails.Message + $posmsg
206+
$InputObject.ErrorDetails.Message + $posmsg
169207
}
170208
}
171-
}
209+
}

0 commit comments

Comments
 (0)