Skip to content

Commit 1ed8c6f

Browse files
committed
Polish a few things in documentation
Add (and document) -recurse option for views Add an example one-line view in the readme Add an explicit license
1 parent 7c86467 commit 1ed8c6f

File tree

4 files changed

+167
-20
lines changed

4 files changed

+167
-20
lines changed

ErrorView.psd1

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,36 @@
55
CompanyName = 'PoshCode'
66

77
ModuleToProcess = 'ErrorView.psm1'
8-
ModuleVersion = '0.0.1'
8+
ModuleVersion = '0.0.2'
99

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

12-
FunctionsToExport = 'Format-Error', 'Write-NativeCommandError', 'ConvertTo-CategoryErrorView', 'ConvertTo-NormalErrorView', 'ConvertTo-SimpleErrorView'
12+
FunctionsToExport = 'Format-Error', 'Set-ErrorView', 'Write-NativeCommandError', 'ConvertTo-CategoryErrorView', 'ConvertTo-NormalErrorView', 'ConvertTo-SimpleErrorView', 'ConvertTo-FullErrorView'
1313

1414
CmdletsToExport = @()
1515
VariablesToExport = @()
1616
AliasesToExport = @("fe")
1717

18-
PrivateData = @{
19-
PSData = @{
20-
# Tags applied to this module. These help with module discovery in online galleries.
21-
# Tags = @()
18+
PrivateData = @{
19+
PSData = @{
20+
# Tags applied to this module. These help with module discovery in online galleries.
21+
Tags = @("ErrorView")
2222

23-
# A URL to the license for this module.
24-
# LicenseUri = ''
23+
# A URL to the license for this module.
24+
LicenseUri = "http://opensource.org/licenses/MIT"
2525

26-
# A URL to the main website for this project.
27-
# ProjectUri = ''
26+
# A URL to the main website for this project.
27+
ProjectUri = 'https://github.com/poshcode/errorview'
2828

29-
# A URL to an icon representing this module.
30-
# IconUri = ''
29+
# A URL to an icon representing this module.
30+
# IconUri = ''
3131

32-
# ReleaseNotes of this module
33-
ReleaseNotes = 'This module exists for testing ideas for ErrorViews'
32+
# ReleaseNotes of this module
33+
ReleaseNotes = 'I wrote this module to enhace ErrorViews for Windows PowerShell (without waiting for PS7+)'
3434

35-
} # End of PSData hashtable
35+
} # End of PSData hashtable
3636

37-
} # End of PrivateData hashtable
37+
} # End of PrivateData hashtable
3838

39-
# HelpInfoURI = ''
4039
}
4140

ErrorView.psm1

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@ param(
77
Update-FormatData -PrependPath $PSScriptRoot\ErrorView.ps1xml
88

99
function Format-Error {
10+
<#
11+
.SYNOPSIS
12+
Formats an error for the screen using a custom error view
13+
.DESCRIPTION
14+
Temporarily switches the error view and outputs the errors
15+
.EXAMPLE
16+
Format-Error
17+
18+
Shows the Normal error view for the most recent error
19+
.EXAMPLE
20+
$error[0..4] | Format-Error Full
21+
22+
Shows the full error view (like using | Format-List * -Force) for the most recent 5 errors
23+
.EXAMPLE
24+
$error[3] | Format-Error Full -Recurse
25+
26+
Shows the full error view of the specific error, recursing into the inner exceptions (if that's supported by the view)
27+
#>
1028
[CmdletBinding()]
1129
[Alias("fe")]
1230
[OutputType([System.Management.Automation.ErrorRecord])]
1331
param(
32+
# The name of the ErrorView you want to use (there must a matching ConvertTo-${View}ErrorView function)
1433
[Parameter(Position=0, ValueFromPipelineByPropertyName)]
1534
[ArgumentCompleter({
1635
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
@@ -20,21 +39,56 @@ function Format-Error {
2039
})]
2140
$View = "Normal",
2241

42+
# Error records (e.g. from $Error). Defaults to the most recent error: $Error[0]
2343
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
24-
[System.Management.Automation.ErrorRecord]$InputObject
44+
[Alias("ErrorRecord")]
45+
[System.Management.Automation.ErrorRecord]$InputObject = $(
46+
$e = $global:Error[0]
47+
if ($e -is ([System.Management.Automation.ErrorRecord])) { $e }
48+
elseif ($e.ErrorRecord -is ([System.Management.Automation.ErrorRecord])) { $e.ErrorRecord }
49+
elseif ($global:Error.Count -eq 0) { Write-Warning "The global `$Error collection is empty" }
50+
),
2551

52+
# Allows ErrorView functions to recurse to InnerException
53+
[switch]$Recurse
2654
)
2755
begin {
56+
$ErrorActionPreference = "Continue"
2857
$View, $global:ErrorView = $ErrorView, $View
58+
[bool]$Recurse, [bool]$global:ErrorViewRecurse = [bool]$global:ErrorViewRecurse, $Recurse
2959
}
3060
process {
3161
$InputObject
3262
}
3363
end {
64+
[bool]$global:ErrorViewRecurse = $Recurse
3465
$global:ErrorView = $View
3566
}
3667
}
3768

69+
function Set-ErrorView {
70+
<#
71+
.SYNOPSIS
72+
A helper function to provide tab-completion for error view names
73+
#>
74+
[CmdletBinding()]
75+
[Alias("fe")]
76+
[OutputType([System.Management.Automation.ErrorRecord])]
77+
param(
78+
# The name of the ErrorView you want to use (there must a matching ConvertTo-${View}ErrorView function)
79+
[Parameter(Position = 0, ValueFromPipelineByPropertyName)]
80+
[ArgumentCompleter( {
81+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
82+
[System.Management.Automation.CompletionResult[]]((
83+
Get-Command ConvertTo-*ErrorView -ListImported -ParameterName InputObject -ParameterType [System.Management.Automation.ErrorRecord]
84+
).Name -replace "ConvertTo-(.*)ErrorView", '$1' -like "*$($wordToComplete)*")
85+
})]
86+
$View = "Normal"
87+
)
88+
$global:ErrorView = $View
89+
}
90+
91+
3892
function Write-NativeCommandError {
3993
[CmdletBinding()]
4094
param(
@@ -207,3 +261,25 @@ function ConvertTo-NormalErrorView {
207261
}
208262
}
209263
}
264+
265+
function ConvertTo-FullErrorView {
266+
[CmdletBinding()]
267+
param(
268+
[System.Management.Automation.ErrorRecord]
269+
$InputObject
270+
)
271+
272+
$Detail = $InputObject | Format-List * -Force | Out-String
273+
274+
# NOTE: ErrorViewRecurse is normally false, and only set temporarily by Format-Error -Recurse
275+
if ($ErrorViewRecurse) {
276+
$Count = 1
277+
$Exception = $InputObject.Exception
278+
while ($Exception = $Exception.InnerException) {
279+
$Detail += "`nINNER EXCEPTION $($Count): $($Exception.GetType().FullName)`n`n"
280+
$Detail += $Exception | Format-List * -Force | Out-String
281+
$Count++
282+
}
283+
}
284+
$Detail
285+
}

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2015 Joel Bennett
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

ReadMe.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,60 @@ This is a _very_ simple module. It exports one important command: `Format-Error`
77
* `ConvertTo-CategoryErrorView`
88
* `ConvertTo-NormalErrorView`
99
* `ConvertTo-SimpleErrorView`
10+
* `ConvertTo-FullErrorView`
1011

11-
But more importantly, _when it's imported_, it allows you to override PowerShell's error formatting by setting the global `$ErrorView` variable to something like "SingleLine" and writing a function `ConvertTo-SingleLineErrorView` (i.e. `ConvertTo-<name>ErrorView`), and of course, it provides a "Simple" error view on top of PowerShell's existing "Normal" and "Category" views.
12+
### Install it from PSGallery
1213

13-
![Some Examples of Formatted Errors](Resources/screenshot.png "Make sure you try Format-Error!")
14+
```powershell
15+
Install-Module ErrorView
16+
```
17+
18+
_When it's imported_, it sets PowerShell's global `$ErrorView` variable to "Simple" and provides an implementation as `ConvertTo-SimpleErrorView`. **More importantly**, it allows you to write your own ErrorView! For example you could write a function or script `ConvertTo-SingleLineErrorView` and then set the preference variable `$ErrorView = "SingleLine"`, or even set it as you import the ErrorView module:
19+
20+
```powershell
21+
Import-Module ErrorView -Args SingleLine
22+
```
23+
24+
NOTE: by default, there is no "SingleLine" view in the ErrorView module. The default after importing the module is the "Simple" error view, which is currently a 2-line view.
25+
26+
![Some Examples of Formatted Errors](Resources/screenshot.png "Make sure you try Format-Error!")
27+
28+
### Format-Error
29+
30+
The `Format-Error` command lets you change the view temporarily to look at more details of errors, and even has a -Recurse switch to let error views show details of inner exceptions. If you have set your view to Simple (which is the default after importing the module), you can see the Normal view for the previous error by just running `Format-Error`. To see more than one error, you can look at the previous 5 errors like: `$error[0..4] | Format-Error`
31+
32+
### Custom Error Views
33+
34+
As stated above, the ErrorView module allows you to write your own formats. Here's an example, and a few pointers:
35+
36+
```powershell
37+
function ConvertTo-SingleLineErrorView {
38+
param( [System.Management.Automation.ErrorRecord]$InputObject )
39+
-join @(
40+
$originInfo = &{ Set-StrictMode -Version 1; $InputObject.OriginInfo }
41+
if (($null -ne $originInfo) -and ($null -ne $originInfo.PSComputerName)) {
42+
"[" + $originInfo.PSComputerName + "]: "
43+
}
44+
45+
$errorDetails = &{ Set-StrictMode -Version 1; $InputObject.ErrorDetails }
46+
if (($null -ne $errorDetails) -and ($null -ne $errorDetails.Message) -and ($InputObject.FullyQualifiedErrorId -ne "NativeCommandErrorMessage")) {
47+
$errorDetails.Message
48+
} else {
49+
$InputObject.Exception.Message
50+
}
51+
52+
if ($ErrorViewRecurse) {
53+
$Prefix = "`n Exception: "
54+
$Exception = &{ Set-StrictMode -Version 1; $InputObject.Exception }
55+
do {
56+
$Prefix + $Exception.GetType().FullName
57+
$Prefix = "`n InnerException: "
58+
} while ($Exception = $Exception.InnerException)
59+
}
60+
)
61+
}
62+
```
63+
64+
1. The function **must** use the `ConvertTo` verb and "ErrorView" noun, with _your view name as the prefix_.
65+
2. The function **must** have an `InputObject` parameter of type `System.Management.Automation.ErrorRecord`
66+
3. There is a new global variable: `$ErrorViewRecurse` which is set by `Format-Error -Recurse`

0 commit comments

Comments
 (0)