60 lines
2.0 KiB
PowerShell
60 lines
2.0 KiB
PowerShell
function Get-LastWebRequestErrorText {
|
|
<#
|
|
.SYNOPSIS
|
|
Finds the last error in the errors stack that has a response stream and reads the value.
|
|
Obviously this only works for open/current sessions, as this is an in memory variable that can be wiped at any time.
|
|
|
|
.PARAMETER All
|
|
[switch] Finds all errors and writes them out serially from most recent to last
|
|
#>
|
|
[CmdletBinding()]
|
|
[OutputType([string])]
|
|
param (
|
|
[switch]$All
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName)
|
|
$allErrors = @($error)
|
|
|
|
if (($allErrors.Count -eq 0) -or ($allErrors[0] -eq $null)) {
|
|
Write-Host "$logLead : No errors found or first entry is null. This is unexpected. Can not continue."
|
|
return $null
|
|
}
|
|
|
|
# Setup some place to track the values we found
|
|
$foundStreams = @()
|
|
|
|
# Ensure each error has been processed to see if it has a value that can be used
|
|
foreach ($anyError in $allErrors) {
|
|
if ($anyError.ResponseRecorded) {
|
|
$foundStreams += $anyError.ResponseFoundStream
|
|
continue
|
|
}
|
|
|
|
$response = $anyError.Exception.Response
|
|
if ($null -ne $response) {
|
|
# This only happens for things that have a response and that haven't been processed yet
|
|
# That is to say, the cycle spin time here should be short
|
|
$responseStream = $response.GetResponseStream()
|
|
$foundStream = $null
|
|
if ($null -ne $responseStream) {
|
|
$foundStream = (Read-StreamAsString $responseStream)
|
|
$foundStreams += $foundStream
|
|
}
|
|
$anyError | Add-Member -NotePropertyName "ResponseFoundStream" -NotePropertyValue $foundStream
|
|
$anyError | Add-Member -NotePropertyName "ResponseRecorded" -NotePropertyValue $true
|
|
}
|
|
}
|
|
|
|
if ($foundStreams.Count -eq 0) {
|
|
Write-Warning "$logLead : Could not find any errors with response streams to parse."
|
|
return $null
|
|
}
|
|
|
|
if (!$All) {
|
|
return $foundStreams[0]
|
|
} else {
|
|
return $foundStreams
|
|
}
|
|
}
|