Created
June 23, 2010 09:51
-
-
Save johnnyhalife/449714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.Services.Client; | |
using System.Net; | |
public static class ExceptionRetriableExtension | |
{ | |
/// <summary> | |
/// Retry on all exceptions except 2xx, 3xx, 4xx | |
/// The 2xx is there for Table batch operations in which teh status code can be 202 even when | |
/// the call failed | |
/// </summary> | |
/// <param name="currentRetryCount"></param> | |
/// <param name="lastException"></param> | |
/// <param name="retryInterval"></param> | |
/// <returns></returns> | |
public static bool IsExceptionRetryable(this Exception lastException) | |
{ | |
int statusCode = GetStatusCodeFromException(lastException); | |
// Let us not retry if 2xx, 3xx, 4xx, 501 and 505 errors | |
if (statusCode == -1 | |
|| (statusCode >= 200 && statusCode < 500) | |
|| statusCode == (int)HttpStatusCode.NotImplemented | |
|| statusCode == (int)HttpStatusCode.HttpVersionNotSupported) | |
{ | |
return false; | |
} | |
return true; | |
} | |
/// <summary> | |
/// Get the status code from exception. This can be sued for Table, Queue and Blob service | |
/// </summary> | |
/// <param name="e"></param> | |
/// <returns></returns> | |
private static int GetStatusCodeFromException(Exception e) | |
{ | |
// Handle DataService request and client exceptions for Table service | |
DataServiceRequestException dsre = e as DataServiceRequestException; | |
if (dsre != null) | |
{ | |
// Retrieve the status code: | |
// - if we have an operation response, then it is the status code of that operation response. | |
// We can only have one response on failure and we can ignore the batch status | |
// - otherwise it is the batch status code | |
var response = dsre.Response; | |
if (response != null) | |
{ | |
return response.BatchStatusCode; | |
} | |
} | |
DataServiceClientException dsce = e as DataServiceClientException; | |
if (dsce != null) | |
{ | |
return dsce.StatusCode; | |
} | |
WebException we = e as WebException; | |
if (we != null) | |
{ | |
HttpWebResponse response = we.Response as HttpWebResponse; | |
// if we do not get a response, we will assume bad gateway. This is not completely true, but since it | |
// is better to retry on such errors, we make up an error code here | |
return response != null ? (int)response.StatusCode : (int)HttpStatusCode.BadGateway; | |
} | |
// let us not retry on any other exceptions | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment