@@ -361,52 +361,56 @@ private DocumentCollection GetCollectionIfExists(string databaseName, string col
361
361
public static async Task < V > ExecuteWithRetries < V > ( DocumentClient client , Func < Task < V > > function , bool shouldLogRetries = false )
362
362
{
363
363
TimeSpan sleepTime = TimeSpan . Zero ;
364
+ int [ ] expectedStatusCodes = new int [ ] { 429 , 400 , 503 } ;
364
365
365
366
while ( true )
366
367
{
367
368
try
368
369
{
369
370
return await function ( ) ;
370
371
}
371
- catch ( DocumentClientException de )
372
- {
373
- if ( ( int ) de . StatusCode != 429 && ( int ) de . StatusCode != 400 && ( int ) de . StatusCode != 503 )
374
- {
375
- Trace . TraceError ( de . ToString ( ) ) ;
376
- throw de ;
377
- }
378
-
379
- sleepTime = de . RetryAfter ;
380
- }
381
372
catch ( System . Net . Http . HttpRequestException )
382
373
{
383
374
sleepTime = TimeSpan . FromSeconds ( 1 ) ;
384
375
}
385
- catch ( AggregateException ae )
376
+ catch ( Exception e )
386
377
{
387
- if ( ! ( ae . InnerException is DocumentClientException ) )
378
+ DocumentClientException de ;
379
+ if ( ! TryExtractDocumentClientException ( e , out de ) )
388
380
{
389
- Trace . TraceError ( ae . ToString ( ) ) ;
390
381
throw ;
391
382
}
392
383
393
- DocumentClientException de = ( DocumentClientException ) ae . InnerException ;
394
- if ( ( int ) de . StatusCode != 429 && ( int ) de . StatusCode != 400 && ( int ) de . StatusCode != 503 )
384
+ sleepTime = de . RetryAfter ;
385
+ if ( shouldLogRetries )
395
386
{
396
- Trace . TraceError ( de . ToString ( ) ) ;
397
- throw de ;
387
+ Console . WriteLine ( "Retrying after sleeping for {0}" , sleepTime ) ;
398
388
}
399
-
400
- sleepTime = de . RetryAfter ;
401
389
}
402
390
403
- if ( shouldLogRetries )
391
+ await Task . Delay ( sleepTime ) ;
392
+ }
393
+ }
394
+
395
+ private static bool TryExtractDocumentClientException ( Exception e , out DocumentClientException de )
396
+ {
397
+ if ( e is DocumentClientException )
398
+ {
399
+ de = ( DocumentClientException ) e ;
400
+ return true ;
401
+ }
402
+
403
+ if ( e is AggregateException )
404
+ {
405
+ if ( e . InnerException is DocumentClientException )
404
406
{
405
- Console . WriteLine ( "Retrying after sleeping for {0}" , sleepTime ) ;
407
+ de = ( DocumentClientException ) e . InnerException ;
408
+ return true ;
406
409
}
407
-
408
- await Task . Delay ( sleepTime ) ;
409
410
}
411
+
412
+ de = null ;
413
+ return false ;
410
414
}
411
415
}
412
416
}
0 commit comments