Public/Write-ConnectorError.ps1

<#
.SYNOPSIS
    Sends an error log entry to the Fortytwo IAM Core Connector API and optionally throws an exception.

.DESCRIPTION
    Posts a timestamped log message to the connector's log endpoint in the Fortytwo IAM platform.
    Useful for recording sync progress or errors that should be visible in the platform.
    Failures to send are surfaced as warnings rather than terminating errors.

.PARAMETER Text
    The log message to send.

.PARAMETER InnerException
    The exception that caused the error, if any.

.PARAMETER Throw
    If specified, the function will throw an exception after logging the error.

.EXAMPLE
    Write-ConnectorError -Text "Sync failed"

.EXAMPLE
    try {} catch {Write-ConnectorError -Text "Failed to process user" -InnerException $_ -Throw}
#>

function Write-ConnectorError {
    [CmdletBinding()]

    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string] $Text,
        
        [Parameter(Mandatory = $false, Position = 1)]
        $InnerException,

        [Parameter(Mandatory = $false, Position = 2)]
        [switch] $Throw
    )

    Process {
        Send-ConnectorLog -Text $Text -Type "Error"

        if ($Throw) {
            if ($InnerException.GetType().FullName -eq "System.Exception") {
                throw [System.Exception]::new($Text, $InnerException)    
            }
            else {
                throw [System.Exception]::new("$($Text): $InnerException")
            }            
        }
    }
}