internal/functions/other/Invoke-TerminatingException.ps1
|
function Invoke-TerminatingException { <# .SYNOPSIS Throw a terminating exception in the context of the caller. .DESCRIPTION Throw a terminating exception in the context of the caller. Masks the actual code location from the end user in how the message will be displayed. .PARAMETER Cmdlet The $PSCmdlet variable of the calling command. .PARAMETER Message The message to show the user. The exception built from it is marked as the module's own text and passes the sanitizer unchecked, so it holds only text of the module (strings.psd1) and values that are safe already: identifiers, the safe text of a failure ([PSMicrosoftEntraID.Security.ErrorSanitizer]::MessageFor). Never server text, a request, an answer or the message of a foreign exception. The error codes, status and request id of a safe inner exception are carried over, so a later sanitizer keeps them. .PARAMETER Exception A nested exception to include in the exception object. A foreign one is replaced by its safe form ([PSMicrosoftEntraID.Security.ErrorSanitizer]::Sanitize). .PARAMETER Category The category of the error. .PARAMETER ErrorRecord A full error record that was caught by the caller. Use this when you want to rethrow an existing error. .EXAMPLE PS C:\> Invoke-TerminatingException -Cmdlet $PSCmdlet -Message 'Unknown calling module' Terminates the calling command, citing an unknown caller. #> [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] $Cmdlet, [string] $Message, [System.Exception] $Exception, [System.Management.Automation.ErrorCategory] $Category = [System.Management.Automation.ErrorCategory]::NotSpecified, [System.Management.Automation.ErrorRecord] $ErrorRecord ) process { # The record or exception of a failed web call holds the answer's body and the request with its token: it is # passed on only as the safe record of ConvertTo-EntraSafeErrorRecord (the original leaves $Error). if ($ErrorRecord -and [PSMicrosoftEntraID.Security.ErrorSanitizer]::CarriesHttpExchange($ErrorRecord)) { $ErrorRecord = ConvertTo-EntraSafeErrorRecord -ErrorRecord $ErrorRecord } if ($Exception) { $Exception = [PSMicrosoftEntraID.Security.ErrorSanitizer]::Sanitize($Exception) } if ($ErrorRecord -and -not $Message) { $Cmdlet.ThrowTerminatingError($ErrorRecord) } # Under a message of the module, a foreign exception is nested in its safe form only, and its record leaves $Error. if ($ErrorRecord) { $Exception = [PSMicrosoftEntraID.Security.ErrorSanitizer]::Sanitize($ErrorRecord.Exception) Clear-EntraErrorEntry -ErrorRecord $ErrorRecord } $exceptionType = switch ($Category) { default { [System.Exception] } 'InvalidArgument' { [System.ArgumentException] } 'InvalidData' { [System.IO.InvalidDataException] } 'AuthenticationError' { [System.Security.Authentication.AuthenticationException] } 'InvalidOperation' { [System.InvalidOperationException] } } if ($Exception) { try { $newException = $Exception.GetType()::new($Message, $Exception) } catch { $newException = [System.Exception]::new($Message, $Exception) } } else { $newException = $exceptionType::new($Message) } # The message is the module's own; the diagnostics of the safe inner exception are kept beside it. $newException.Data[[PSMicrosoftEntraID.Security.ErrorSanitizer]::SafeMessageKey] = $true if ($Exception) { foreach ($key in 'ErrorCode', 'StatusCode', 'RequestId', 'RetryAfterSeconds', 'OriginalType') { if ($Exception.Data.Contains($key)) { $newException.Data[$key] = $Exception.Data[$key] } } } $record = [System.Management.Automation.ErrorRecord]::new($newException, (Get-PSCallStack)[1].FunctionName, $Category, $Target) $Cmdlet.ThrowTerminatingError($record) } } |