1
1
param (
2
- [ArgumentCompleter ({ (Get-Command ConvertTo-Error * - ListImported).Name -replace " ConvertTo-Error(?:View)?(.*)(?:View)" , ' $1' })]
3
- $ErrorView
2
+ $global :ErrorView = " Simple"
4
3
)
5
4
6
5
# We need to overwrite the ErrorView
7
6
# So -PrependPath, instead of FormatsToProcess
8
7
Update-FormatData - PrependPath $PSScriptRoot \ErrorView.ps1xml
9
8
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
+
10
38
function Write-NativeCommandError {
11
39
[CmdletBinding ()]
12
40
param (
13
41
[System.Management.Automation.ErrorRecord ]
14
- $CurrentError
42
+ $InputObject
15
43
)
16
44
17
- if ($CurrentError .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) { return }
45
+ if ($InputObject .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) { return }
18
46
19
- $myinv = $CurrentError .InvocationInfo
47
+ $myinv = $InputObject .InvocationInfo
20
48
if ($myinv -and $myinv.MyCommand ) {
21
49
switch - regex ( $myinv.MyCommand.CommandType ) {
22
50
([System.Management.Automation.CommandTypes ]::ExternalScript) {
@@ -46,29 +74,38 @@ function Write-NativeCommandError {
46
74
$myinv.InvocationName + " : "
47
75
}
48
76
}
49
- function ConvertTo-ErrorCategoryView {
77
+
78
+ function ConvertTo-CategoryErrorView {
50
79
[CmdletBinding ()]
51
80
param (
52
81
[System.Management.Automation.ErrorRecord ]
53
- $CurrentError
82
+ $InputObject
54
83
)
55
84
56
- $CurrentError .CategoryInfo.GetMessage ()
85
+ $InputObject .CategoryInfo.GetMessage ()
57
86
}
58
87
59
- function ConvertTo-ErrorSimpleView {
88
+ function ConvertTo-SimpleErrorView {
60
89
[CmdletBinding ()]
61
90
param (
62
91
[System.Management.Automation.ErrorRecord ]
63
- $CurrentError
92
+ $InputObject
64
93
)
65
94
66
- if ($CurrentError .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
67
- $CurrentError .Exception.Message
95
+ if ($InputObject .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
96
+ $InputObject .Exception.Message
68
97
} 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
+ }
72
109
} else {
73
110
$posmsg = " "
74
111
}
@@ -77,14 +114,14 @@ function ConvertTo-ErrorSimpleView {
77
114
$posmsg = " `n " + $posmsg
78
115
}
79
116
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
82
119
}
83
120
84
121
$indent = 4
85
122
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
86
123
87
- $originInfo = & { Set-StrictMode - Version 1 ; $CurrentError .OriginInfo }
124
+ $originInfo = & { Set-StrictMode - Version 1 ; $InputObject .OriginInfo }
88
125
if (($null -ne $originInfo ) -and ($null -ne $originInfo.PSComputerName )) {
89
126
$indentString = " + PSComputerName : " + $originInfo.PSComputerName
90
127
$posmsg += " `n "
@@ -95,25 +132,26 @@ function ConvertTo-ErrorSimpleView {
95
132
}
96
133
}
97
134
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 "
100
137
} else {
101
- $CurrentError .ErrorDetails.Message + $posmsg
138
+ $InputObject .ErrorDetails.Message + $posmsg
102
139
}
103
140
}
104
141
}
105
- function ConvertTo-ErrorNormalView {
142
+
143
+ function ConvertTo-NormalErrorView {
106
144
[CmdletBinding ()]
107
145
param (
108
146
[System.Management.Automation.ErrorRecord ]
109
- $CurrentError
147
+ $InputObject
110
148
)
111
149
112
- if ($CurrentError .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
113
- $CurrentError .Exception.Message
150
+ if ($InputObject .FullyQualifiedErrorId -eq " NativeCommandErrorMessage" ) {
151
+ $InputObject .Exception.Message
114
152
} 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' ))) {
117
155
$posmsg = $myinv.PositionMessage
118
156
} else {
119
157
$posmsg = " "
@@ -123,18 +161,18 @@ function ConvertTo-ErrorNormalView {
123
161
$posmsg = " `n " + $posmsg
124
162
}
125
163
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
128
166
}
129
167
130
168
$indent = 4
131
169
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
132
170
133
- $errorCategoryMsg = & { Set-StrictMode - Version 1 ; $CurrentError .ErrorCategory_Message }
171
+ $errorCategoryMsg = & { Set-StrictMode - Version 1 ; $InputObject .ErrorCategory_Message }
134
172
if ($null -ne $errorCategoryMsg ) {
135
- $indentString = " + CategoryInfo : " + $CurrentError .ErrorCategory_Message
173
+ $indentString = " + CategoryInfo : " + $InputObject .ErrorCategory_Message
136
174
} else {
137
- $indentString = " + CategoryInfo : " + $CurrentError .CategoryInfo
175
+ $indentString = " + CategoryInfo : " + $InputObject .CategoryInfo
138
176
}
139
177
$posmsg += " `n "
140
178
foreach ($line in @ ($indentString -split " (.{$width })" )) {
@@ -143,15 +181,15 @@ function ConvertTo-ErrorNormalView {
143
181
}
144
182
}
145
183
146
- $indentString = " + FullyQualifiedErrorId : " + $CurrentError .FullyQualifiedErrorId
184
+ $indentString = " + FullyQualifiedErrorId : " + $InputObject .FullyQualifiedErrorId
147
185
$posmsg += " `n "
148
186
foreach ($line in @ ($indentString -split " (.{$width })" )) {
149
187
if ($line ) {
150
188
$posmsg += (" " * $indent + $line )
151
189
}
152
190
}
153
191
154
- $originInfo = & { Set-StrictMode - Version 1 ; $CurrentError .OriginInfo }
192
+ $originInfo = & { Set-StrictMode - Version 1 ; $InputObject .OriginInfo }
155
193
if (($null -ne $originInfo ) -and ($null -ne $originInfo.PSComputerName )) {
156
194
$indentString = " + PSComputerName : " + $originInfo.PSComputerName
157
195
$posmsg += " `n "
@@ -162,10 +200,10 @@ function ConvertTo-ErrorNormalView {
162
200
}
163
201
}
164
202
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 "
167
205
} else {
168
- $CurrentError .ErrorDetails.Message + $posmsg
206
+ $InputObject .ErrorDetails.Message + $posmsg
169
207
}
170
208
}
171
- }
209
+ }
0 commit comments