protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var response = await base.SendAsync(request, cancellationToken);

            if (response.IsSuccessStatusCode == false &&
                response.Content?.Headers?.ContentType?.MediaType?.Equals(MediaTypes.Application.Json) == true)
            {
                RestError err  = null;
                string    json = null;
                try
                {
                    json = await response.Content.ReadAsStringAsync();

                    err = JsonConvert.DeserializeObject <RestError>(json);
                }
                catch
                {
                    // we are currently handling exception, don't throw another one during deserialization
                }

                if (err?.Type != null)
                {
                    var ex = ApiException.FromRestError(err, response);

                    // fill additional exception properties
                    JsonConvert.PopulateObject(json, ex);

                    throw ex;
                }
            }

            return(response);
        }
Example #2
0
        public static ApiException FromRestError(RestError restError, HttpResponseMessage response)
        {
            var ctor = GetConstructor(restError.Type);

            if (ctor != null)
            {
                return(ctor(restError.Type, restError.Message, response));
            }
            else
            {
                return(new ApiException(restError.Type, restError.Message, response));
            }
        }