CosmosDB icon indicating copy to clipboard operation
CosmosDB copied to clipboard

When using Get-CosmosDBDocument, getting error "Cannot index into a null array" when error 429 is returned

Open gwpelletier opened this issue 5 years ago • 2 comments

Issue

When using the function Get-CosmosDbDocument, getting below error instead of expected behavior when a 429 error is returned

PS > $Error[0]
Cannot index into a null array.
At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.5.2.487\CosmosDB.psm1:5463 char:17
+ ...             [System.Int32] $retryAfter = ($_.Exception.Response.Heade ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Expected Behavior

When error 429 is returned, Get-CosmosDBDocument should return the error UNLESS a backoff policy is supplied

PS > $Error[0]
Invoke-WebRequest : The remote server returned an error: (429) Too Many Requests.
At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\3.5.2.487\CosmosDB.psm1:5452 char:30
+ ...        $requestResult = Invoke-WebRequest @invokeWebRequestParameters
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Investigation

Root cause: Appears to be a problem with how the headers table is being accessed. I updated the line to this and the issue is resolved:

[System.Int32] $retryAfter = $_.Exception.Response.Headers['x-ms-retry-after-ms']

PSVersion Table:

PS > $PSVersionTable

Name                           Value                                                                                                                                       
----                           -----                                                                                                                                       
PSVersion                      5.1.17763.1007                                                                                                                              
PSEdition                      Desktop                                                                                                                                     
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                     
BuildVersion                   10.0.17763.1007                                                                                                                             
CLRVersion                     4.0.30319.42000                                                                                                                             
WSManStackVersion              3.0                                                                                                                                         
PSRemotingProtocolVersion      2.3                                                                                                                                         
SerializationVersion           1.1.0.1            

Installed module:

PS > Get-InstalledModule CosmosDB | Select Name,Version,Repository

Name     Version   Repository
----     -------   ----------
CosmosDB 3.5.2.487 PSGallery 

gwpelletier avatar Mar 18 '20 00:03 gwpelletier

Thanks for raising this @gwpelletier - I think I've come across a related issue to this before. Depending on the .NET version (.NET/.NET Core) being used, the headers behave differently. So I'll update this to try and handle multiple formats for the header.

PlagueHO avatar Mar 18 '20 22:03 PlagueHO

Hi, We have come across this same error with Invoke-CosmosDbStoredProcedure. We call the cmdlet as part of a do...while loop.

Cannot index into a null array.
At C:\Users\DavidO'Brien\Documents\WindowsPowerShell\Modules\CosmosDB\4.5.0\CosmosDB.psm1:844 char:17
+ ...             [System.Int32] $retryAfter = ($_.Exception.Response.Heade ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Line 844 in the module is:

[System.Int32] $retryAfter = ($_.Exception.Response.Headers | Where-Object -Property Key -eq 'x-ms-retry-after-ms').Value[0]

It throws that same error a couple of times and then exits the script.

davidobrien1985 avatar Mar 03 '22 00:03 davidobrien1985