From ad0b82c9c69c4423010a10bd567a615e9b2b32a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Mon, 7 Dec 2020 23:07:11 +0300 Subject: [PATCH] Add additional catch --- EosSharp/EosSharp/HttpHelper.cs | 45 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 092e754..6d04e9f 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -223,31 +223,52 @@ public async Task BuildSendResponse(HttpResponseMessage response) var stream = await response.Content.ReadAsStreamAsync(); if (response.IsSuccessStatusCode) return stream; - var content = await StreamToStringAsync(stream); - if(string.IsNullOrEmpty(content)) + string content = null; + try { - throw new NullReferenceException($"Couldn't parse stream data. Content: {content}, Status code: {response.StatusCode}"); + content = await StreamToStringAsync(stream); } - - ApiErrorException apiError = null; - try + catch (System.Exception ex) { - apiError = JsonConvert.DeserializeObject(content); + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = $"Couldn't parse stream data. exception: {ex.ToString()}" + }; } - catch(Exception) + + if(string.IsNullOrEmpty(content)) { throw new ApiException { StatusCode = (int)response.StatusCode, - Content = content + Content = $"Couldn't parse stream data." }; } - if(apiError == null) + try + { + ApiErrorException apiError = JsonConvert.DeserializeObject(content); + + if(apiError == null) + { + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = $"Api error is null! Status code: {response.StatusCode}, content: {content}" + }; + } + + throw apiError; + } + catch(Exception ex) { - throw new NullReferenceException($"Api error is null! Response was: {content}"); + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = $"Couldn't deserialized api error, exception: {ex.ToString()}, content: {content}" + }; } - throw apiError; } ///