protected async Task <ErrorResponse> HandleTransientErrorResponse(HttpResponseMessage response)
        {
            if (DefaultSettings.TransientErrors.Contains(response.StatusCode))
            {
                throw new TransientException(response.StatusCode);
            }

            string result = await response.Content?.ReadAsStringAsync();

            if (string.IsNullOrEmpty(result))
            {
                return(new ErrorResponse {
                    ErrorMessage = response.StatusCode.ToString()
                });
            }

            if (!response.IsSuccessStatusCode)
            {
                try
                {
                    // if an error causes HTML to be returned, deserialization fails.
                    return(ModelConverter.Deserialize <ErrorResponse>(result));
                }
                catch (Exception)
                {
                    return(ErrorResponse.FromError(result));
                }
            }

            return(null);
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);

            if (!response.IsSuccessStatusCode)
            {
                string result = await response.Content?.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(result))
                {
                    ErrorResponse error;
                    try
                    {
                        // if BitGo throws a 404, then the content is not JSON, it's HTML.
                        // which makes deserialization fail
                        error = _modelConverter.Deserialize <ErrorResponse>(result);
                    }
                    catch (System.Exception)
                    {
                        error = ErrorResponse.FromError(result);
                    }

                    _logger.LogError("An HTTP {errorCode} error occurred during BitGo Request: {@error}", response.StatusCode, error);
                }
                else
                {
                    _logger.LogError("An HTTP {errorCode} error occurred during BitGo Request: {@error}", response.StatusCode, response.ReasonPhrase);
                }
            }

            return(response);
        }