Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Fixed exceptions for non-json responses #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions IotaCSharpApi/Api/Exception/IotaApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ public class IotaApiException : System.Exception
public IotaApiException(string error) : base(error)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="IotaApiException"/> class.
/// </summary>
/// <param name="message">The exception message.</param>
/// <param name="innerException">The inner exception.</param>
public IotaApiException(string message, System.Exception innerException)
: base(message, innerException)
{
}
}
}
15 changes: 11 additions & 4 deletions IotaCSharpApi/Api/Utils/Rest/JsonWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ public TResponse GetPOSTResponseSync<TResponse>(Uri uri, string data)
}
catch (WebException ex)
{
Console.WriteLine("catched: " + ex.ToString() + ex.Message);

using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
String errorResponse = reader.ReadToEnd();
throw new IotaApiException(JsonConvert.DeserializeObject<ErrorResponse>(errorResponse).Error);
var errorResponse = reader.ReadToEnd();
string deserialized;
try
{
deserialized = JsonConvert.DeserializeObject<ErrorResponse>(errorResponse).Error;
}
catch (System.Exception)
{
deserialized = "Caught WebException but was unable to deserialize content (no JSON).";
}
throw new IotaApiException(deserialized, ex);
}
}
}
Expand Down