Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active May 13, 2018 13:08
Show Gist options
  • Select an option

  • Save Jaykul/aee1a55d25f260a605c1 to your computer and use it in GitHub Desktop.

Select an option

Save Jaykul/aee1a55d25f260a605c1 to your computer and use it in GitHub Desktop.
WriteError or Write-Error versus ThrowTerminatingError and Throw
PS$ @'
>> [CmdletBinding()]param()
>>
>> Write-Host "Imagine you're doing some work and you call a command from my module..."
>> Test-Failures "Throw" -ErrorAction Continue
>> '@ > ~\Documents\WindowsPowerShell\Scripts\TestFailureMode.ps1
PS$ ~\Documents\WindowsPowerShell\Scripts\TestFailureMode.ps1
Imagine you're doing some work and you call a command from my module...
This error was thrown on line 11 from Test-Failures in the FailureMode module
At C:\Users\Jaykul\Projects\Modules\FailureMode.psm1:11 char:17
+ ... throw "This error was thrown on line 11 from Test-Failure ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (This error was ...lureMode module:String) [], RuntimeException
+ FullyQualifiedErrorId : This error was thrown on line 11 from Test-Failures in the FailureMode module
PS$ @'
>> [CmdletBinding()]param()
>>
>> Write-Host "Imagine you're doing some work and you call a command from my module..."
>> Test-Failures "CmdletThrow" -ErrorAction Continue
>> '@ > ~\Documents\WindowsPowerShell\Scripts\TestFailureMode.ps1
PS$ ~\Documents\WindowsPowerShell\Scripts\TestFailureMode.ps1
Imagine you're doing some work and you call a command from my module...
Test-Failures : This error was WriteError -Terminating on line 24 from Test-Failures in the FailureMode module
At C:\Users\Jaykul\Documents\WindowsPowerShell\Scripts\TestFailureMode.ps1:4 char:1
+ Test-Failures "CmdletThrow" -ErrorAction Continue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Test-Failures], AggregateException
+ FullyQualifiedErrorId : ExampleTerminating,FailureMode\Test-Failures,Test-Failures
function Test-Failures {
[CmdletBinding()]
param(
[ValidateSet("Throw","WriteError","CmdletThrow","CmdletError")]
$Type
)
end {
Write-Verbose "ENTER Test-Failures:End Line 8"
switch($Type) {
"Throw" {
throw "This error was thrown on line 11 from Test-Failures in the FailureMode module"
}
"WriteError" {
Write-Error "This error was Write-Error on line 14 from Test-Failures in the FailureMode module"
}
"CmdletError" {
WriteError -ExceptionType System.AggregateException `
-Message "This error was WriteError on line 18 from Test-Failures in the FailureMode module" `
-ErrorId "ExampleTerminating,FailureMode\Test-Failures" `
-Category "NotSpecified"
}
"CmdletThrow" {
WriteError -ExceptionType System.AggregateException `
-Message "This error was WriteError -Terminating on line 24 from Test-Failures in the FailureMode module" `
-ErrorId "ExampleTerminating,FailureMode\Test-Failures" `
-Category "NotSpecified" `
-Terminating
}
}
Write-Verbose "EXIT Test-Failures:End Line "
}
}
function Test-Trap {
[CmdletBinding()]
param(
[ValidateSet("Throw","WriteError","CmdletThrow","CmdletError")]
$NestedType,
[ValidateSet("Throw","WriteError","CmdletThrow","CmdletError")]
$Type
)
end {
trap {
$e = $_
if($e -is [System.Management.Automation.ErrorRecord]) {
$e = $e.Exception
}
switch($Type) {
"Throw" {
throw $e
}
"WriteError" {
Write-Error $e
}
"CmdletError" {
WriteError -Exception $e `
-Message "This error was WriteError on line 58 from Test-Failures in the FailureMode module" `
-ErrorId "ExampleTerminating,FailureMode\Test-Failures" `
-Category "NotSpecified"
}
"CmdletThrow" {
WriteError -Exception $e `
-Message "This error was WriteError -Terminating on line 67 from Test-Failures in the FailureMode module" `
-ErrorId "ExampleTerminating,FailureMode\Test-Failures" `
-Category "NotSpecified" `
-Terminating
}
}
}
Test-Failures -Type $NestedType
}
}
function Test-ErrorAction {
[CmdletBinding()]
param(
[ValidateSet("Throw","WriteError","CmdletThrow","CmdletError")]
$Type
)
end {
Test-Failures -Type $Type -ErrorAction Stop
}
}
# Utility to throw an errorrecord
function WriteError {
param
(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCmdlet]
$Cmdlet = $((Get-Variable -Scope 1 PSCmdlet).Value),
[Parameter(Mandatory = $true, ParameterSetName="ExistingException", Position=1, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Parameter(ParameterSetName="NewException")]
[ValidateNotNullOrEmpty()]
[System.Exception]
$Exception,
[Parameter(ParameterSetName="NewException", Position=2)]
[ValidateNotNullOrEmpty()]
[System.String]
$ExceptionType="System.Management.Automation.RuntimeException",
[Parameter(Mandatory = $true, ParameterSetName="NewException", Position=3)]
[ValidateNotNullOrEmpty()]
[System.String]
$Message,
[Parameter(Mandatory = $false)]
[System.Object]
$TargetObject,
[Parameter(Mandatory = $true, Position=10)]
[ValidateNotNullOrEmpty()]
[System.String]
$ErrorId,
[Parameter(Mandatory = $true, Position=11)]
[ValidateNotNull()]
[System.Management.Automation.ErrorCategory]
$Category,
[Parameter(Mandatory = $true, ParameterSetName="Rethrow", Position=1)]
[System.Management.Automation.ErrorRecord]$ErrorRecord,
[Switch]$Terminating
)
process {
if(!$ErrorRecord) {
if($PSCmdlet.ParameterSetName -eq "NewException") {
if($Exception) {
$Exception = New-Object $ExceptionType $Message, $Exception
} else {
$Exception = New-Object $ExceptionType $Message
}
}
$errorRecord = New-Object System.Management.Automation.ErrorRecord $Exception, $ErrorId, $Category, $TargetObject
}
if($Terminating) {
$Cmdlet.ThrowTerminatingError($errorRecord)
} else {
$Cmdlet.WriteError($errorRecord)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment