Skip to content

Commit

Permalink
Adding Posh.Errors
Browse files Browse the repository at this point in the history
Fixes #4, Fixes #18, Fixes #19, Fixes #20, Fixes #21, Fixes #22
  • Loading branch information
James Brundage committed Jul 20, 2023
1 parent f6071ac commit a0d2bac
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Types/Posh.Errors/LineLike.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<#
.SYNOPSIS
Filters errors by line wildcard
.DESCRIPTION
Filters entries in $Error for items that a .Line like a wildcard.
.EXAMPLE
$error.LineLike("Get-*")
#>
param(
# A wildcard pattern.
[string]
$Like
)

,@(foreach ($err in $this) {
if ($err.InvocationInfo.Line -like $Like) {
$err
}
})
18 changes: 18 additions & 0 deletions Types/Posh.Errors/LineMatch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<#
.SYNOPSIS
Filters errors by command pattern
.DESCRIPTION
Filters entries in $Error for items that have a .CommandLine matching the pattern.
.EXAMPLE
$error.LineMatc("^Get")
#>
param(
# A regular expression.
$Match
)

,@(foreach ($err in $this) {
if ($err.InvocationInfo.Line -match $match) {
$err
}
})
25 changes: 25 additions & 0 deletions Types/Posh.Errors/get_ByLine.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Gets errors by line
.DESCRIPTION
Gets all lines that produced errors
.EXAMPLE
$error.ByLine
#>
$errorsByLine =
[Collections.Generic.Dictionary[
string,
[Collections.Generic.List[psobject]]
]]::new([System.StringComparer]::OrdinalIgnoreCase)


foreach ($err in $this) {

$errLine = $err.InvocationInfo.Line
if (-not $errLine) { continue }
if (-not $errorsByLine[$errLine]) {
$errorsByLine[$errLine] = [Collections.Generic.List[psobject]]::new()
}
$null = $errorsByLine[$errLine].Add($err)
}
$errorsByLine
23 changes: 23 additions & 0 deletions Types/Posh.Errors/get_ByType.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<#
#>
$errorsByType =
[Collections.Generic.Dictionary[
type,
[Collections.Generic.List[psobject]]
]]::new()


foreach ($err in $this) {

$exceptionType = if ($err.Exception) {
$err.Exception.GetType()
} else {
$err.GetType()
}
if (-not $errorsByType[$exceptionType]) {
$errorsByType[$exceptionType] = [Collections.Generic.List[psobject]]::new()
}
$null = $errorsByType[$exceptionType].Add($err)
}
$errorsByType
15 changes: 15 additions & 0 deletions Types/Posh.Errors/get_History.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<#
.SYNOPSIS
Gets Error History
.DESCRIPTION
Gets the history items associated with PowerShell Errors
.EXAMPLE
$Error.History
#>
$historyIDS = @(foreach ($err in $this) {
if ($err.InvocationInfo.HistoryId -gt 0) {
$err.InvocationInfo.HistoryId
}
})

Get-History -Id $historyIDS

0 comments on commit a0d2bac

Please sign in to comment.