From a0d2bac02b05563e93a75cd871e440cceffdd9d8 Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Thu, 20 Jul 2023 00:28:29 -0700 Subject: [PATCH] Adding Posh.Errors Fixes #4, Fixes #18, Fixes #19, Fixes #20, Fixes #21, Fixes #22 --- Types/Posh.Errors/LineLike.ps1 | 19 +++++++++++++++++++ Types/Posh.Errors/LineMatch.ps1 | 18 ++++++++++++++++++ Types/Posh.Errors/get_ByLine.ps1 | 25 +++++++++++++++++++++++++ Types/Posh.Errors/get_ByType.ps1 | 23 +++++++++++++++++++++++ Types/Posh.Errors/get_History.ps1 | 15 +++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 Types/Posh.Errors/LineLike.ps1 create mode 100644 Types/Posh.Errors/LineMatch.ps1 create mode 100644 Types/Posh.Errors/get_ByLine.ps1 create mode 100644 Types/Posh.Errors/get_ByType.ps1 create mode 100644 Types/Posh.Errors/get_History.ps1 diff --git a/Types/Posh.Errors/LineLike.ps1 b/Types/Posh.Errors/LineLike.ps1 new file mode 100644 index 0000000..9422f01 --- /dev/null +++ b/Types/Posh.Errors/LineLike.ps1 @@ -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 + } +}) \ No newline at end of file diff --git a/Types/Posh.Errors/LineMatch.ps1 b/Types/Posh.Errors/LineMatch.ps1 new file mode 100644 index 0000000..68d551a --- /dev/null +++ b/Types/Posh.Errors/LineMatch.ps1 @@ -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 + } +}) \ No newline at end of file diff --git a/Types/Posh.Errors/get_ByLine.ps1 b/Types/Posh.Errors/get_ByLine.ps1 new file mode 100644 index 0000000..b2f6ad8 --- /dev/null +++ b/Types/Posh.Errors/get_ByLine.ps1 @@ -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 diff --git a/Types/Posh.Errors/get_ByType.ps1 b/Types/Posh.Errors/get_ByType.ps1 new file mode 100644 index 0000000..63d83b6 --- /dev/null +++ b/Types/Posh.Errors/get_ByType.ps1 @@ -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 diff --git a/Types/Posh.Errors/get_History.ps1 b/Types/Posh.Errors/get_History.ps1 new file mode 100644 index 0000000..667c435 --- /dev/null +++ b/Types/Posh.Errors/get_History.ps1 @@ -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 \ No newline at end of file