Private/Resolve-LMException.ps1

#Function to throttle API requests exceeding limits
Function Resolve-LMException {
    Param (
        $LMException
    )
    #Add in rate limit handeling
    If ($LMException.Exception.Response.StatusCode.value__ -eq 429) {
        $RateLimitWindow = $LMException.Exception.Response.Headers.Value[$LMException.Exception.Response.Headers.Key.IndexOf("x-rate-limit-window")]
        $RateLimitSize = $LMException.Exception.Response.Headers.Value[$LMException.Exception.Response.Headers.Key.IndexOf("x-rate-limit-limit")]
        #$RateLimitRemaining = $LMException.Exception.Response.Headers.Value[$LMException.Exception.Response.Headers.Key.IndexOf("x-rate-limit-remaining")]

        Write-LMHost "Request exceeded rate limit window of $RateLimitSize requests over $RateLimitWindow seconds, retrying operation in $RateLimitWindow seconds" -ForegroundColor Yellow
        Start-Sleep -Seconds $RateLimitWindow
        return $true
    }
    #Handle all other errors
    ELse {
        Switch ($LMException.Exception.GetType().FullName) {
            { "System.Net.WebException" -or "Microsoft.PowerShell.Commands.HttpResponseException" } {
                $HttpException = ($LMException.ErrorDetails.Message | ConvertFrom-Json).errorMessage
                If (!$HttpException) {
                    $HttpException = ($LMException.ErrorDetails.Message | ConvertFrom-Json).message
                }
                $HttpStatusCode = $LMException.Exception.Response.StatusCode.value__
                #Output to null so ErrorAction works as expected
                Write-Error "Failed to execute web request($($HttpStatusCode)): $HttpException" 2>$null
                #Write to $Error object but supress so stack trace is not displayed
                Write-Error "Failed to execute web request($($HttpStatusCode)): $HttpException" -ErrorAction SilentlyContinue
                #Pretty print error to console
                If($ErrorActionPreference -ne "SilentlyContinue"){
                    [Console]::ForegroundColor = 'red'
                    [Console]::Error.WriteLine("Failed to execute web request($($HttpStatusCode)): $HttpException")
                    [Console]::ResetColor()
                    #throw "Failed to execute web request($($HttpStatusCode)): $HttpException"
                }
            }
            default {
                $LMError = $LMException.ToString()
                #Output to null so ErrorAction works as expected
                Write-Error "Failed to execute web request: $LMError" 2>$null
                #Write to $Error object but supress so stack trace is not displayed
                Write-Error "Failed to execute web request: $LMError" -ErrorAction SilentlyContinue
                #Pretty print error to console
                If($ErrorActionPreference -ne "SilentlyContinue"){
                    [Console]::ForegroundColor = 'red'
                    [Console]::Error.WriteLine("Failed to execute web request: $LMError")
                    [Console]::ResetColor()
                    #throw "Failed to execute web request: $LMError"
                }
            }
        }
        Return $false
    }
}