lib/icinga/exception/Exit-IcingaThrowException.psm1
function Exit-IcingaThrowException() { param( [string]$InputString, [string]$StringPattern, [string]$CustomMessage, [string]$ExceptionThrown, [ValidateSet('Permission','Input','Configuration','Unhandled','Custom')] [string]$ExceptionType = 'Unhandled', [switch]$Force ); if ($Force -eq $FALSE) { if ($null -eq $InputString -Or [string]::IsNullOrEmpty($InputString)) { return; } if (-Not $InputString.Contains($StringPattern)) { return; } } $ExceptionMessageLib = $null; $ExceptionTypeString = ''; switch ($ExceptionType) { 'Permission' { $ExceptionTypeString = 'Permission'; $ExceptionMessageLib = $IcingaExceptions.Permission; }; 'Input' { $ExceptionTypeString = 'Invalid Input'; $ExceptionMessageLib = $IcingaExceptions.Inputs; }; 'Configuration' { $ExceptionTypeString = 'Invalid Configuration'; $ExceptionMessageLib = $IcingaExceptions.Configuration; }; 'Unhandled' { $ExceptionTypeString = 'Unhandled'; }; 'Custom' { $ExceptionTypeString = 'Custom'; }; } [string]$ExceptionName = ''; if ($null -ne $ExceptionMessageLib) { foreach ($definedError in $ExceptionMessageLib.Keys) { if ($ExceptionMessageLib.$definedError -eq $ExceptionThrown) { $ExceptionName = $definedError; break; } } } else { $ExceptionName = [string]::Format('{0} Exception', $ExceptionTypeString); $ExceptionThrown = [string]::Format( '{0} exception occured:{1}{2}', $ExceptionTypeString, "`r`n", $InputString ); } $OutputMessage = '{0}: Icinga {5} Error was thrown: {3}: {4}{1}{1}{2}'; if ([string]::IsNullOrEmpty($CustomMessage) -eq $TRUE) { $OutputMessage = '{0}: Icinga {5} Error was thrown: {3}{1}{1}{2}{4}'; } $OutputMessage = [string]::Format( $OutputMessage, $IcingaEnums.IcingaExitCodeText.($IcingaEnums.IcingaExitCode.Unknown), "`r`n", $ExceptionThrown, $ExceptionName, $CustomMessage, $ExceptionTypeString ); if ($global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) { Write-IcingaConsolePlain $OutputMessage; exit $IcingaEnums.IcingaExitCode.Unknown; } } |