internal static async Task <HttpCallResponse <T> > SendAsync <T>(IRequestBuilder <T> builder, HttpMethod method, CancellationToken cancellationToken = default) { var settings = builder.GetSettings(); settings.OnBeforeSend(builder, builder.Inner); var request = builder.Inner.Message; request.Method = method; Exception exception = null; HttpResponseMessage response = null; try { using (settings.ProfileRequest?.Invoke(request)) { using (request) { using (response = await settings.ClientPool.Get(builder.Inner).SendAsync(request, cancellationToken)) { if (!response.IsSuccessStatusCode && !builder.Inner.IgnoreResponseStatuses.Contains(response.StatusCode)) { exception = new HttpClientException( $"Response code was {(int)response.StatusCode} ({response.StatusCode}) from {response.RequestMessage.RequestUri}: {response.ReasonPhrase}", response.StatusCode, response.RequestMessage.RequestUri); stackTraceString.SetValue(exception, new StackTrace(true).ToString()); } else { var data = await builder.Handler(response); return(HttpCallResponse.Create(response, data)); } } } } } catch (TaskCanceledException ex) { exception = cancellationToken.IsCancellationRequested ? new HttpClientException($"HttpClient request cancelled by token request.", builder.Inner.Message.RequestUri, ex) : new HttpClientException($"HttpClient request time out: {builder.Inner.Timeout.TotalMilliseconds:N0}ms", builder.Inner.Message.RequestUri, ex); } catch (Exception ex) { exception = ex; } var ret = response == null ? HttpCallResponse.Create <T>(request, exception) : HttpCallResponse.Create <T>(response, exception); (builder.Inner as HttpBuilder)?.AddExceptionData(exception); if (builder.Inner.LogErrors) { var args = new HttpExceptionArgs(builder.Inner, exception); builder.Inner.OnBeforeExceptionLog(args); if (!args.AbortLogging) { settings.OnException(builder, args); } } return(ret); }
public void OnBeforeExceptionLog(HttpExceptionArgs args) { BeforeExceptionLog?.Invoke(this, args); }
internal void OnException(object sender, HttpExceptionArgs args) => Exception?.Invoke(sender, args);