/// <summary> /// Converts to exceptions. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="restResponse">The rest response.</param> /// <param name="response">The response.</param> /// <exception cref="RestException">Error calling REST API. {restResponse.ErrorMessage}</exception> /// <exception cref="RestException{T}">Error calling REST API. {restResponse.Content}</exception> /// <exception cref="Trident.Rest.RestException">Error calling REST API. {restResponse.ErrorMessage}</exception> private static void NormalizeErrorsToExceptions <T>(IRestResponse restResponse, Rest.RestResponse <T> response) { if (restResponse.ErrorException != null) { throw new RestException(response, $"Error calling REST API. {restResponse.ErrorMessage}. Check inner exception for details.", restResponse.ErrorException); } if ((int)restResponse.StatusCode >= 300 || (int)restResponse.StatusCode < 200) { throw new RestException <T>(response, $"Error calling REST API. {restResponse.Content}"); } }
public EventResponse(Rest.RestResponse restResponse) : base(restResponse.Status, restResponse.Message, restResponse.Response) { Route = restResponse.Route; Time = restResponse.Time; }
public async Task <Stream> SendAsync(RestRequest request) { int id = Interlocked.Increment(ref nextId); #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Start"); #endif LastAttemptAt = DateTimeOffset.UtcNow; while (true) { await _queue.EnterGlobalAsync(id, request).ConfigureAwait(false); await EnterAsync(id, request).ConfigureAwait(false); if (_redirectBucket != null) { return(await _redirectBucket.SendAsync(request)); } #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Sending..."); #endif RateLimitInfo info = default(RateLimitInfo); try { Rest.RestResponse response = await request.SendAsync().ConfigureAwait(false); info = new RateLimitInfo(response.Headers); if (response.StatusCode < (HttpStatusCode)200 || response.StatusCode >= (HttpStatusCode)300) { switch (response.StatusCode) { case (HttpStatusCode)429: if (info.IsGlobal) { #if DEBUG_LIMITS Debug.WriteLine($"[{id}] (!) 429 [Global]"); #endif _queue.PauseGlobal(info); } else { #if DEBUG_LIMITS Debug.WriteLine($"[{id}] (!) 429"); #endif UpdateRateLimit(id, request, info, true); } await _queue.RaiseRateLimitTriggered(Id, info, $"{request.Method} {request.Endpoint}").ConfigureAwait(false); continue; //Retry case HttpStatusCode.BadGateway: //502 #if DEBUG_LIMITS Debug.WriteLine($"[{id}] (!) 502"); #endif if ((request.Options.RetryMode & RetryMode.Retry502) == 0) { throw new HttpException(HttpStatusCode.BadGateway, request, null); } continue; //Retry default: int? code = null; string reason = null; if (response.Stream != null) { try { using (StreamReader reader = new StreamReader(response.Stream)) using (JsonTextReader jsonReader = new JsonTextReader(reader)) { JToken json = JToken.Load(jsonReader); try { code = json.Value <int>("code"); } catch { }; try { reason = json.Value <string>("message"); } catch { }; } } catch { } } throw new HttpException(response.StatusCode, request, code, reason); } } else { #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Success"); #endif return(response.Stream); } } //catch (HttpException) { throw; } //Pass through catch (TimeoutException) { #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Timeout"); #endif if ((request.Options.RetryMode & RetryMode.RetryTimeouts) == 0) { throw; } await Task.Delay(500).ConfigureAwait(false); continue; //Retry } /*catch (Exception) * { #if DEBUG_LIMITS * Debug.WriteLine($"[{id}] Error"); #endif * if ((request.Options.RetryMode & RetryMode.RetryErrors) == 0) * throw; * * await Task.Delay(500); * continue; //Retry * }*/ finally { UpdateRateLimit(id, request, info, false); #if DEBUG_LIMITS Debug.WriteLine($"[{id}] Stop"); #endif } } }