internal static Exception GetException(HttpHelper httpHelper, object result)
{
if (result == null)
return null;
var responseDict = result as IDictionary<string, object>;
if (responseDict == null)
return null;
FacebookApiException resultException = null;
if (httpHelper != null)
{
var response = httpHelper.HttpWebResponse;
var responseUri = response.ResponseUri;
// legacy rest api
if (responseUri.Host == "api.facebook.com" ||
responseUri.Host == "api-read.facebook.com" ||
responseUri.Host == "api-video.facebook.com" ||
responseUri.Host == "api.beta.facebook.com" ||
responseUri.Host == "api-read.beta.facebook.com" ||
responseUri.Host == "api-video.facebook.com")
{
if (responseDict.ContainsKey("error_code"))
{
string errorCode = responseDict["error_code"].ToString();
string errorMsg = null;
if (responseDict.ContainsKey("error_msg"))
errorMsg = responseDict["error_msg"] as string;
// Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
if (errorCode == "190")
resultException = new FacebookOAuthException(errorMsg, errorCode);
else if (errorCode == "4" || errorCode == "API_EC_TOO_MANY_CALLS" ||
(errorMsg != null && errorMsg.Contains("request limit reached")))
resultException = new FacebookApiLimitException(errorMsg, errorCode);
else
resultException = new FacebookApiException(errorMsg, errorCode);
return resultException;
}
return null;
}
}
// graph api error
if (responseDict.ContainsKey("error"))
{
var error = responseDict["error"] as IDictionary<string, object>;
if (error != null)
{
var errorType = error["type"] as string;
var errorMessage = error["message"] as string;
int errorCode = 0;
if (error.ContainsKey("code"))
int.TryParse(error["code"].ToString(), out errorCode);
var errorSubcode = 0;
if (error.ContainsKey("error_subcode"))
int.TryParse(error["error_subcode"].ToString(), out errorSubcode);
string errorUserTitle = null;
if (error.ContainsKey("error_user_title"))
errorUserTitle = (string)error["error_user_title"];
string errorUserMsg = null;
if (error.ContainsKey("error_user_msg"))
errorUserMsg = (string)error["error_user_msg"];
// Check to make sure the correct data is in the response
if (!string.IsNullOrEmpty(errorType) && !string.IsNullOrEmpty(errorMessage))
{
// We don't include the inner exception because it is not needed and is always a WebException.
// It is easier to understand the error if we use Facebook's error message.
if (errorType == "OAuthException")
resultException = new FacebookOAuthException(errorMessage, errorType, errorCode, errorSubcode);
else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage.Contains("request limit reached")))
resultException = new FacebookApiLimitException(errorMessage, errorType);
else
resultException = new FacebookApiException(errorMessage, errorType, errorCode, errorSubcode);
}
resultException.ErrorUserTitle = errorUserTitle;
resultException.ErrorUserMsg = errorUserMsg;
}
else
{
long? errorNumber = null;
if (responseDict["error"] is long)
errorNumber = (long)responseDict["error"];
if (errorNumber == null && responseDict["error"] is int)
errorNumber = (int)responseDict["error"];
string errorDescription = null;
if (responseDict.ContainsKey("error_description"))
errorDescription = responseDict["error_description"] as string;
if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
{
if (errorNumber == 190)
resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
else
resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString(CultureInfo.InvariantCulture));
}
}
}
return resultException;
}