// Map incorrect parameter errors internal static T MapParameterError <T>(this ITwitterSession s, string errorMessage) where T : new() { var responseMessage = new T(); var twitterControlMessage = new TwitterControlMessage { twitter_error_message = String.Format("Parameter Error: {0}", errorMessage) }; var boolType = new[] { typeof(bool) }; var tcmType = new[] { typeof(TwitterControlMessage) }; // find the property set method created by the compiler // and manually set the right params in the response var statusProperty = responseMessage.GetType().GetRuntimeMethod("set_twitterFaulted", boolType); if (statusProperty != null) { statusProperty.Invoke(responseMessage, new object[] { true }); } var tcmProperty = responseMessage.GetType().GetRuntimeMethod("set_twitterControlMessage", tcmType); if (tcmProperty != null) { tcmProperty.Invoke(responseMessage, new object[] { twitterControlMessage }); } return(responseMessage); }
public static void PrintTwitterErrors(TwitterControlMessage tcm) { ConsoleOutput.PrintMessage("START: TWITTER CONTROL MESSAGE"); ConsoleOutput.PrintError(String.Format("http reason: {0}", tcm.http_reason)); ConsoleOutput.PrintError(String.Format("http status code: {0}", tcm.http_status_code)); ConsoleOutput.PrintError(String.Format("twitter error code: {0}", tcm.twitter_error_code)); ConsoleOutput.PrintError(String.Format("twitter error message: {0}", tcm.twitter_error_message)); ConsoleOutput.PrintError(String.Format("API rates: {0}/{1} Resets {2}", tcm.twitter_rate_limit_remaining, tcm.twitter_rate_limit_limit, tcm.twitter_rate_limit_reset)); ConsoleOutput.PrintMessage("END: TWITTER CONTROL MESSAGE"); }
// Map error results in the TwitterControlMessage internal static TwitterControlMessage MapHTTPResponses(this Task <HttpResponseMessage> m) { var twitterControlMessage = new TwitterControlMessage(); try { var bodyContent = m.Result.Content.ReadAsStringAsync(); /* {StatusCode: 429, ReasonPhrase: 'Too Many Requests', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:*/ twitterControlMessage.http_status_code = IntValueForHTTPHeaderKey("StatusCode", m.Result.ToString()); twitterControlMessage.twitter_rate_limit_limit = IntValueForHTTPHeaderKey("x-rate-limit-limit", m.Result.ToString()); twitterControlMessage.twitter_rate_limit_remaining = IntValueForHTTPHeaderKey("x-rate-limit-remaining", m.Result.ToString()); twitterControlMessage.twitter_rate_limit_reset = DateTimeForHTTPHeaderKey("x-rate-limit-reset", m.Result.ToString()); // when posting images, these responses may appear twitterControlMessage.twitter_mediaratelimit_limit = IntValueForHTTPHeaderKey("x-mediaratelimit-limit", m.Result.ToString()); twitterControlMessage.twitter_mediaratelimit_remaining = IntValueForHTTPHeaderKey("x-mediaratelimit-remaining", m.Result.ToString()); twitterControlMessage.twitter_mediaratelimit_class = StringValueForHTTPHeaderKey("x-mediaratelimit-class", m.Result.ToString()); if (twitterControlMessage.http_status_code == 429) { twitterControlMessage.twitter_error_message = "Rate Limit Exceeded"; } else { twitterControlMessage.http_reason = StringValueForHTTPHeaderKey("ReasonPhrase", m.Result.ToString()); } var errordetail = JsonConvert.DeserializeObject <TwitterError>(bodyContent.Result); if (errordetail != null) { twitterControlMessage.twitter_error_message = errordetail.errors[0].message; twitterControlMessage.twitter_error_code = errordetail.errors[0].code; } } catch (Exception e) { twitterControlMessage.twitter_error_message = e.Message; twitterControlMessage.twitter_error_code = 42; } return(twitterControlMessage); }