private/tests/Write-ZtTestError.ps1

function Write-ZtTestError {
    <#
    .SYNOPSIS
        Write the error that happened in a test.
 
    .DESCRIPTION
        Write the error that happened in a test.
        This handles the logging of an error, as well as updating the markdown.
 
    .PARAMETER Result
        The result object of the test.
        Used by Get-ZtTestStatistics.
 
    .PARAMETER Test
        The test object for the test that failed.
 
    .PARAMETER ErrorRecord
        The Error that happened.
 
    .EXAMPLE
        PS C:\> Write-ZtTestError -Test $this -Result $result -ErrorRecord $_
 
        Writes the error that happened in the specified test.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        $Result,

        [Parameter(Mandatory = $true)]
        $Test,

        $ErrorRecord
    )
    process {
        Write-PSFMessage -Level Warning -Message "Error executing test '{0}': {1}" -StringValues $Test.TestID, $ErrorRecord.Exception.Message -Target $Test -ErrorRecord $ErrorRecord
        $Result.Success = $false
        $Result.Error = $ErrorRecord
        $message = @(
            '❌ Test {0} failed due to an unexpected error.' -f $Test.TestID
            ' - **Error Message**: {0}.' -f $ErrorRecord.Exception.Message
            '```'
            '{0}' -f ($ErrorRecord | Get-Error | Out-String)
            '```'
        ) -join "`r`n"
        Add-ZtTestResultDetail -TestId $Test.TestID -Title $Test.Title -Status $false -Result $message -CustomStatus 'Error'
        Update-ZtProgressState -WorkerId $Test.TestID -WorkerName $Result.DisplayName -WorkerStatus 'Error' -WorkerDetail "Error: $($ErrorRecord.Exception.Message)"
    }
}