From 54f8771a9cd9c3c65c456da49510ddf7aa1ae29f Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 6 Feb 2017 09:15:10 -0500 Subject: [PATCH] better error parsing --- src/App/Repositories/BaseApiRepository.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/App/Repositories/BaseApiRepository.cs b/src/App/Repositories/BaseApiRepository.cs index 45c3c304e..fdbe3fdb6 100644 --- a/src/App/Repositories/BaseApiRepository.cs +++ b/src/App/Repositories/BaseApiRepository.cs @@ -8,6 +8,7 @@ using Plugin.Connectivity.Abstractions; using Bit.App.Abstractions; using System.Net; using XLabs.Ioc; +using Newtonsoft.Json.Linq; namespace Bit.App.Repositories { @@ -191,7 +192,17 @@ namespace Bit.App.Repositories if(statusCode >= 400 && statusCode <= 500) { var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - var errorResponseModel = JsonConvert.DeserializeObject(responseContent); + + ErrorResponse errorResponseModel = null; + var errorResponse = JObject.Parse(responseContent); + if(errorResponse["ErrorModel"] != null && errorResponse["ErrorModel"]["Message"] != null) + { + errorResponseModel = errorResponse["ErrorModel"].ToObject(); + } + else if(errorResponse["Message"] != null) + { + errorResponseModel = errorResponse.ToObject(); + } if(errorResponseModel != null) { @@ -210,10 +221,11 @@ namespace Bit.App.Repositories errors.Add(new ApiError { Message = errorResponseModel.Message }); } } - else - { - errors.Add(new ApiError { Message = "An unknown error has occured." }); - } + } + + if(errors.Count == 0) + { + errors.Add(new ApiError { Message = "An unknown error has occured." }); } return errors;