Forum Discussion
Handling CSOM 429 Errors from PowerShell Scripts
Hope you have to get Retry-After field from catched error object.
try
{
.......................
}
catch
{
Write-Host "Retry-After:" $_.Exception.Response.Headers["Retry-After"]
}
Hey Kevin, I put a breakpoint in my catch block.
$_ looks good (it's the correct exception), but $_.Response is null.
Hit Line breakpoint on 'C:\Users\..\Desktop\Testing\LVT Test.ps1:44'
07:35:59 .\Desktop > $_.Exception.Response.Headers["Retry-After"]
Cannot index into a null array.
At line:1 char:1
+ $_.Exception.Response.Headers["Retry-After"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
07:36:05 .\Desktop > $_
Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an
error: (429)."
At C:\Users\..\Desktop\Testing\LVT
Test.ps1:39 char:9
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
07:36:13 .\Desktop > $_.ResPonse
07:36:22 .\Desktop > $_.ResPonse -eq $null
True
07:36:44 .\Desktop >
I tried to get the Response details with:
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$ErrResp = $streamReader.ReadToEnd()
$streamReader.Close()
Write-Verbose $ErrResp
This get me the string '429 TOO MANY REQUESTS' which might be workable to identify it as a 429 (I wonder if it will always be in English..?). I don't see the response Retry-After.
- Kevin_MorganSep 06, 2019Iron Contributor
Have you checked the below posts :
https://stackoverflow.com/questions/51730540/how-to-get-retry-after-header-in-csom-context
- Cauldron_of_PenguinsSep 06, 2019Copper Contributor
Hey Kevin_Morgan
I had seen that. I have not decorated my requests in my test script, primarily because I am trying to cause 429's there!
I have also not decorated the real script. I should, and will, but this only reduces the probability of receiving a 429. The process for this script (necessarily) enumerates all the libraries in the tenant and updates them. This tenant is huuuuge.
So I feel that, even with a well decorated script, I will still get 429s and need to handle them.
I'm looking at two options:
1) sleep in a suitable location. How long? If I sleep too frequently and too long, the script will run much longer than necessary; If I don't sleep frequently or long enough then I don't solve the problem. MS returns the header for a good reason.
2) Switch to REST. I believe REST will give me the headers, though my quick testing seems to be giving me 406's instead of 429's.