Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Clean up error handling code in client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanvalentin committed May 19, 2014
1 parent ccf649a commit 94e3669
Showing 1 changed file with 65 additions and 70 deletions.
135 changes: 65 additions & 70 deletions DisqusApiPortable/DisqusApiPortable/V30/DsqHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public async Task<T> GetDsqDataAsync<T>(string requestUri)
if (this.DefaultRequestHeaders.Contains("ContentType"))
this.DefaultRequestHeaders.Remove("ContentType");

string errorMessage = "";
int? code = null;
FaultType fault = FaultType.Undetermined;
DsqApiException apiException = null;

try
{
Expand All @@ -62,47 +60,22 @@ public async Task<T> GetDsqDataAsync<T>(string requestUri)
}
else
{
try
{
string raw = await response.Content.ReadAsStringAsync();

JObject json = JObject.Parse(raw);

errorMessage = (string)json["response"];
code = (int)json["code"];
fault = GetFaultType(code ?? 0);
}
catch (Exception ex)
{
errorMessage = response.Content.ReadAsStringAsync().Result + ";" + ex.Message;
code = (int)response.StatusCode;
fault = GetFaultType(code ?? 0);
}
apiException = await GetExceptionFromResponse(response);
}
}
catch (OperationCanceledException ex)
{
if (!ex.CancellationToken.IsCancellationRequested)
{
errorMessage = ex.Message;
code = 16;
fault = FaultType.Timeout;
}
else
{
errorMessage = ex.Message;
code = null;
fault = FaultType.ClientRequest;
}
apiException = GetTaskCancelledException(ex);
}
catch (Exception ex)
{
errorMessage = ex.Message;
code = null;
fault = FaultType.Undetermined;
apiException = new DsqApiException(
ex.Message,
null,
FaultType.Undetermined);
}

throw new DsqApiException(errorMessage, null, GetFaultType(code ?? 0));
throw apiException;
}

/// <summary>
Expand All @@ -121,9 +94,8 @@ public async Task<T> PostDsqDataAsync<T>(string requestUri, List<KeyValuePair<st
if (!this.DefaultRequestHeaders.Contains("ContentType"))
this.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");

string errorMessage = "";
int? code = null;
FaultType fault = FaultType.Undetermined;

DsqApiException apiException = null;

try
{
Expand All @@ -135,47 +107,22 @@ public async Task<T> PostDsqDataAsync<T>(string requestUri, List<KeyValuePair<st
}
else
{
try
{
string raw = await response.Content.ReadAsStringAsync();

JObject json = JObject.Parse(raw);

errorMessage = (string)json["response"];
code = (int)json["code"];
fault = GetFaultType(code ?? 0);
}
catch (Exception ex)
{
errorMessage = response.Content.ReadAsStringAsync().Result + ";" + ex.Message;
code = (int)response.StatusCode;
fault = GetFaultType(code ?? 0);
}
apiException = await GetExceptionFromResponse(response);
}
}
catch (OperationCanceledException ex)
{
if (!ex.CancellationToken.IsCancellationRequested)
{
errorMessage = ex.Message;
code = 16;
fault = FaultType.Timeout;
}
else
{
errorMessage = ex.Message;
code = null;
fault = FaultType.ClientRequest;
}
apiException = GetTaskCancelledException(ex);
}
catch (Exception ex)
{
errorMessage = ex.Message;
code = null;
fault = FaultType.Undetermined;
apiException = new DsqApiException(
ex.Message,
null,
FaultType.Undetermined);
}

throw new DsqApiException(errorMessage, null, GetFaultType(code ?? 0));
throw apiException;
}

private T DeserializeStreamToObjectAsync<T>(StreamReader stream)
Expand All @@ -190,6 +137,54 @@ private T DeserializeStreamToObjectAsync<T>(StreamReader stream)
}
}

private async Task<DsqApiException> GetExceptionFromResponse(HttpResponseMessage response)
{
DsqApiException apiException = null;

try
{
string raw = await response.Content.ReadAsStringAsync();

JObject json = JObject.Parse(raw);

apiException = new DsqApiException(
(string)json["response"],
(int)json["code"],
GetFaultType((int)json["code"]));
}
catch (Exception ex)
{
apiException = new DsqApiException(
response.Content.ReadAsStringAsync().Result + ";" + ex.Message,
(int)response.StatusCode,
GetFaultType((int)response.StatusCode));
}

return apiException;
}

private DsqApiException GetTaskCancelledException(OperationCanceledException ex)
{
DsqApiException apiException = null;

if (!ex.CancellationToken.IsCancellationRequested)
{
apiException = new DsqApiException(
ex.Message,
16,
FaultType.Timeout);
}
else
{
apiException = new DsqApiException(
ex.Message,
null,
FaultType.ClientRequest);
}

return apiException;
}

private FaultType GetFaultType(int errorOrStatusCode)
{
switch (errorOrStatusCode)
Expand Down

0 comments on commit 94e3669

Please sign in to comment.