Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional catch #3

Merged
merged 1 commit into from
Dec 7, 2020
Merged
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
45 changes: 33 additions & 12 deletions EosSharp/EosSharp/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,52 @@ public async Task<Stream> 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<ApiErrorException>(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<ApiErrorException>(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;
}

/// <summary>
Expand Down