From 7a28b41c7c97fee93d1f48b92cf9c5596507e5c4 Mon Sep 17 00:00:00 2001 From: Tom Riedl Date: Tue, 26 Dec 2017 23:27:17 +0100 Subject: [PATCH 1/3] Keep origin exceptions --- IotaCSharpApi/Api/Exception/IotaApiException.cs | 10 ++++++++++ IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/IotaCSharpApi/Api/Exception/IotaApiException.cs b/IotaCSharpApi/Api/Exception/IotaApiException.cs index 1d52a4b..abdced5 100644 --- a/IotaCSharpApi/Api/Exception/IotaApiException.cs +++ b/IotaCSharpApi/Api/Exception/IotaApiException.cs @@ -13,5 +13,15 @@ public class IotaApiException : System.Exception public IotaApiException(string error) : base(error) { } + + /// + /// Initializes a new instance of the class. + /// + /// The exception message. + /// The inner exception. + public IotaApiException(string message, System.Exception innerException) + : base(message, innerException) + { + } } } \ No newline at end of file diff --git a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs index 800b281..25b2d07 100644 --- a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs +++ b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs @@ -60,13 +60,23 @@ public TResponse GetPOSTResponseSync(Uri uri, string data) } catch (WebException ex) { - Console.WriteLine("catched: " + ex.ToString() + ex.Message); + Console.WriteLine("catched: " + ex); using (var stream = ex.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { - String errorResponse = reader.ReadToEnd(); - throw new IotaApiException(JsonConvert.DeserializeObject(errorResponse).Error); + var errorResponse = reader.ReadToEnd(); + string deserialized; + try + { + deserialized = JsonConvert.DeserializeObject(errorResponse).Error; + } + catch (System.Exception) + { + // errorResponse is not a valid JSON + deserialized = errorResponse; + } + throw new IotaApiException(deserialized, ex); } } } From 803eb243e7e4208fa9104317c40d18931c47963f Mon Sep 17 00:00:00 2001 From: Tom Riedl Date: Tue, 26 Dec 2017 23:30:11 +0100 Subject: [PATCH 2/3] No need to pass content --- IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs index 25b2d07..2330d92 100644 --- a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs +++ b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs @@ -73,8 +73,7 @@ public TResponse GetPOSTResponseSync(Uri uri, string data) } catch (System.Exception) { - // errorResponse is not a valid JSON - deserialized = errorResponse; + deserialized = "Caught WebException but was unable to deserialize content (no JSON)."; } throw new IotaApiException(deserialized, ex); } From f392c1b145d166a03c94152ea8172bd955ecebd0 Mon Sep 17 00:00:00 2001 From: Tom Riedl Date: Tue, 26 Dec 2017 23:31:38 +0100 Subject: [PATCH 3/3] Removed console log due to exception best practices: either log or throw, not both --- IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs index 2330d92..0ebcd8a 100644 --- a/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs +++ b/IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs @@ -60,8 +60,6 @@ public TResponse GetPOSTResponseSync(Uri uri, string data) } catch (WebException ex) { - Console.WriteLine("catched: " + ex); - using (var stream = ex.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) {