Skip to content

Commit a642b86

Browse files
Joel BennettJoel Bennett
authored andcommitted
First Draft
0 parents  commit a642b86

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

ErrorView.ps1xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<View>
5+
<Name>ErrorInstance</Name>
6+
<OutOfBand />
7+
<ViewSelectedBy>
8+
<TypeName>System.Management.Automation.ErrorRecord</TypeName>
9+
</ViewSelectedBy>
10+
<CustomControl>
11+
<CustomEntries>
12+
<CustomEntry>
13+
<CustomItem>
14+
<ExpressionBinding>
15+
<ScriptBlock>
16+
Write-NativeCommandError $_
17+
</ScriptBlock>
18+
</ExpressionBinding>
19+
<ExpressionBinding>
20+
<ScriptBlock>
21+
"Hello World"
22+
if ($formatter = Get-Command "ConvertTo-Error$($ErrorView)" -ListImported) {
23+
&amp;$formatter $_
24+
} else {
25+
ConvertTo-ErrorNormalView $_
26+
}
27+
</ScriptBlock>
28+
</ExpressionBinding>
29+
</CustomItem>
30+
</CustomEntry>
31+
</CustomEntries>
32+
</CustomControl>
33+
</View>
34+
</ViewDefinitions>
35+
</Configuration>

ErrorView.psd1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@{
2+
Description = 'Enhances formatting ability for Errors'
3+
GUID = '5857f85c-8a0a-44e1-8da5-c8ff352167e0'
4+
Author = 'Joel Bennett'
5+
CompanyName = 'PoshCode'
6+
7+
RootModule = 'ErrorView.psm1'
8+
FormatsToProcess = 'ErrorView.ps1xml'
9+
ModuleVersion = '0.0.1'
10+
CompatiblePSEditions = @("Core", "Desktop")
11+
12+
Copyright = '(c) Joel Bennett. All rights reserved.'
13+
14+
FunctionsToExport = 'Write-NativeCommandError', 'ConvertTo-ErrorCategoryView', 'ConvertTo-ErrorNormalView', 'ConvertTo-ErrorSimpleView'
15+
16+
CmdletsToExport = @()
17+
VariablesToExport = @()
18+
AliasesToExport = @()
19+
20+
PrivateData = @{
21+
PSData = @{
22+
# Tags applied to this module. These help with module discovery in online galleries.
23+
# Tags = @()
24+
25+
# A URL to the license for this module.
26+
# LicenseUri = ''
27+
28+
# A URL to the main website for this project.
29+
# ProjectUri = ''
30+
31+
# A URL to an icon representing this module.
32+
# IconUri = ''
33+
34+
# ReleaseNotes of this module
35+
# ReleaseNotes = ''
36+
37+
} # End of PSData hashtable
38+
39+
} # End of PrivateData hashtable
40+
41+
# HelpInfoURI = ''
42+
}
43+

ErrorView.psm1

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
function Write-NativeCommandError {
2+
[CmdletBinding()]
3+
param(
4+
[System.Management.Automation.ErrorRecord]
5+
$CurrentError
6+
)
7+
8+
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") { return }
9+
10+
$myinv = $CurrentError.InvocationInfo
11+
if ($myinv -and $myinv.MyCommand) {
12+
switch -regex ( $myinv.MyCommand.CommandType ) {
13+
([System.Management.Automation.CommandTypes]::ExternalScript) {
14+
if ($myinv.MyCommand.Path) {
15+
$myinv.MyCommand.Path + " : "
16+
}
17+
break
18+
}
19+
([System.Management.Automation.CommandTypes]::Script) {
20+
if ($myinv.MyCommand.ScriptBlock) {
21+
$myinv.MyCommand.ScriptBlock.ToString() + " : "
22+
}
23+
break
24+
}
25+
default {
26+
if ($myinv.InvocationName -match '^[&amp;\.]?$') {
27+
if ($myinv.MyCommand.Name) {
28+
$myinv.MyCommand.Name + " : "
29+
}
30+
} else {
31+
$myinv.InvocationName + " : "
32+
}
33+
break
34+
}
35+
}
36+
} elseif ($myinv -and $myinv.InvocationName) {
37+
$myinv.InvocationName + " : "
38+
}
39+
}
40+
41+
function ConvertTo-ErrorCategoryView {
42+
[CmdletBinding()]
43+
param(
44+
[System.Management.Automation.ErrorRecord]
45+
$CurrentError
46+
)
47+
48+
$CurrentError.CategoryInfo.GetMessage()
49+
}
50+
51+
52+
function ConvertTo-ErrorSimpleView {
53+
[CmdletBinding()]
54+
param(
55+
[System.Management.Automation.ErrorRecord]
56+
$CurrentError
57+
)
58+
59+
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
60+
$CurrentError.Exception.Message
61+
} else {
62+
$myinv = $CurrentError.InvocationInfo
63+
if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne 'ParserError'))) {
64+
$posmsg = $myinv.PositionMessage
65+
} else {
66+
$posmsg = ""
67+
}
68+
69+
if ($posmsg -ne "") {
70+
$posmsg = "`n" + $posmsg
71+
}
72+
73+
if ( & { Set-StrictMode -Version 1; $CurrentError.PSMessageDetails } ) {
74+
$posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
75+
}
76+
77+
$indent = 4
78+
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
79+
80+
$originInfo = & { Set-StrictMode -Version 1; $CurrentError.OriginInfo }
81+
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) {
82+
$indentString = "+ PSComputerName : " + $originInfo.PSComputerName
83+
$posmsg += "`n"
84+
foreach ($line in @($indentString -split "(.{$width})")) {
85+
if ($line) {
86+
$posmsg += (" " * $indent + $line)
87+
}
88+
}
89+
}
90+
91+
if (!$CurrentError.ErrorDetails -or !$CurrentError.ErrorDetails.Message) {
92+
$CurrentError.Exception.Message + $posmsg + "`n "
93+
} else {
94+
$CurrentError.ErrorDetails.Message + $posmsg
95+
}
96+
}
97+
}
98+
99+
function ConvertTo-ErrorNormalView {
100+
[CmdletBinding()]
101+
param(
102+
[System.Management.Automation.ErrorRecord]
103+
$CurrentError
104+
)
105+
106+
if ($CurrentError.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") {
107+
$CurrentError.Exception.Message
108+
} else {
109+
$myinv = $CurrentError.InvocationInfo
110+
if ($myinv -and ($myinv.MyCommand -or ($CurrentError.CategoryInfo.Category -ne 'ParserError'))) {
111+
$posmsg = $myinv.PositionMessage
112+
} else {
113+
$posmsg = ""
114+
}
115+
116+
if ($posmsg -ne "") {
117+
$posmsg = "`n" + $posmsg
118+
}
119+
120+
if ( &{ Set-StrictMode -Version 1; $CurrentError.PSMessageDetails } ) {
121+
$posmsg = " : " + $CurrentError.PSMessageDetails + $posmsg
122+
}
123+
124+
$indent = 4
125+
$width = $host.UI.RawUI.BufferSize.Width - $indent - 2
126+
127+
$errorCategoryMsg = &{ Set-StrictMode -Version 1; $CurrentError.ErrorCategory_Message }
128+
if ($null -ne $errorCategoryMsg) {
129+
$indentString = "+ CategoryInfo : " + $CurrentError.ErrorCategory_Message
130+
} else {
131+
$indentString = "+ CategoryInfo : " + $CurrentError.CategoryInfo
132+
}
133+
$posmsg += "`n"
134+
foreach ($line in @($indentString -split "(.{$width})")) {
135+
if ($line) {
136+
$posmsg += (" " * $indent + $line)
137+
}
138+
}
139+
140+
$indentString = "+ FullyQualifiedErrorId : " + $CurrentError.FullyQualifiedErrorId
141+
$posmsg += "`n"
142+
foreach ($line in @($indentString -split "(.{$width})")) {
143+
if ($line) {
144+
$posmsg += (" " * $indent + $line)
145+
}
146+
}
147+
148+
$originInfo = &{ Set-StrictMode -Version 1; $CurrentError.OriginInfo }
149+
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) {
150+
$indentString = "+ PSComputerName : " + $originInfo.PSComputerName
151+
$posmsg += "`n"
152+
foreach ($line in @($indentString -split "(.{$width})")) {
153+
if ($line) {
154+
$posmsg += (" " * $indent + $line)
155+
}
156+
}
157+
}
158+
159+
if (!$CurrentError.ErrorDetails -or !$CurrentError.ErrorDetails.Message) {
160+
$CurrentError.Exception.Message + $posmsg + "`n "
161+
} else {
162+
$CurrentError.ErrorDetails.Message + $posmsg
163+
}
164+
}
165+
}

0 commit comments

Comments
 (0)