From 88fa8fa16b88c0b1f3811e37cb63825ec4fc057e Mon Sep 17 00:00:00 2001 From: nazgaul Date: Mon, 2 Mar 2015 13:50:59 +0200 Subject: [PATCH] handle request too large error message --- src/RedDog.Search/Http/ApiConnection.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/RedDog.Search/Http/ApiConnection.cs b/src/RedDog.Search/Http/ApiConnection.cs index f47a1a0..5b3d57d 100644 --- a/src/RedDog.Search/Http/ApiConnection.cs +++ b/src/RedDog.Search/Http/ApiConnection.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Net; using System.Net.Http; @@ -28,8 +29,10 @@ internal ApiConnection(Uri baseUri, string apiKey, HttpClientHandler handler) _client.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=none"); // Configure serialization. - _formatter = new JsonMediaTypeFormatter(); - _formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + _formatter = new JsonMediaTypeFormatter + { + SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()} + }; _formatter.SerializerSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true }); _formatter.SerializerSettings.Converters.Add(new XsdDurationConverter()); } @@ -42,13 +45,13 @@ public Uri BaseUri public static ApiConnection Create(Uri baseUri, string apiKey, IWebProxy proxy = null) { - var handler = new HttpClientHandler() { Proxy = proxy }; + var handler = new HttpClientHandler { Proxy = proxy }; return new ApiConnection(baseUri, apiKey, handler); } public static ApiConnection Create(string serviceName, string apiKey, IWebProxy proxy = null) { - var handler = new HttpClientHandler() { Proxy = proxy }; + var handler = new HttpClientHandler { Proxy = proxy }; return new ApiConnection(new Uri(String.Format(ApiConstants.BaseUrl, serviceName)), apiKey, handler); } @@ -128,6 +131,15 @@ private async Task> BuildResponse(HttpRespons } else { + if (response.StatusCode == HttpStatusCode.RequestEntityTooLarge) + { + response.Error = new Error + { + Message = message.ReasonPhrase, + Code = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture) + }; + return response; + } // Errors should also be deserialized. var errorResponse = await message.Content.ReadAsAsync(cancelToken) .ConfigureAwait(false);