Skip to content

Commit

Permalink
Merge pull request #642 from raistlinthewiz/develop
Browse files Browse the repository at this point in the history
Added more exception handlers to DaemonBase.cs.
  • Loading branch information
Hüseyin Uslu committed Oct 27, 2014
2 parents a4baeb1 + d76f88f commit 6379590
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
78 changes: 54 additions & 24 deletions src/CoiniumServ/Daemon/DaemonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ private HttpWebRequest MakeHttpRequest(DaemonRequest walletRequest)
dataStream.Close();
}
}
catch (WebException exception)
catch (WebException webException)
{
throw _rpcExceptionFactory.GetRpcException(webException);
}
catch (Exception exception)
{
throw _rpcExceptionFactory.GetRpcException(exception);
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while making json request.", exception);
}

return webRequest;
Expand All @@ -173,6 +177,10 @@ private DaemonResponse<T> GetRpcResponse<T>(HttpWebRequest httpWebRequest)
{
throw new Exception("There was a problem deserializing the response from the coin wallet.", jsonEx);
}
catch (Exception exception)
{
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", exception);
}
}

/// <summary>
Expand All @@ -188,12 +196,17 @@ private string GetJsonResponse(HttpWebRequest httpWebRequest)

// Deserialize the json response
using (var stream = webResponse.GetResponseStream())
using (var reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
reader.Close();
if (stream == null)
return string.Empty;

return result;
using (var reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
reader.Close();

return result;
}
}
}
catch (ProtocolViolationException protocolException)
Expand All @@ -207,29 +220,46 @@ private string GetJsonResponse(HttpWebRequest httpWebRequest)
if (response == null)
throw _rpcExceptionFactory.GetRpcException("Error while reading json response", webException);

using (var stream = response.GetResponseStream())
var error = ReadJsonError(response); // try to read the error response.

if(error != null)
throw _rpcExceptionFactory.GetRpcErrorException(error); //throw the error.
else
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", webException);
}
catch (Exception exception)
{
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", exception);
}
}

private RpcErrorResponse ReadJsonError(HttpWebResponse response)
{
using (var stream = response.GetResponseStream())
{
if (stream == null)
return null;

using (var reader = new StreamReader(stream))
{
using (var reader = new StreamReader(stream))
string data = reader.ReadToEnd(); // read the error response.

// we actually expect a json error response here, but it seems some coins may return non-json responses.
try
{
var error = JsonConvert.DeserializeObject<RpcErrorResponse>(data); // so let's try parsing the error response as json.
return error;
}
catch (JsonException e) // if we can't parse the error response as json
{
throw _rpcExceptionFactory.GetRpcException(data, e); // then just use the error text.
}
catch (Exception exception)
{
string error = reader.ReadToEnd(); // read the error response.

// we actually expect a json error response here, but it seems some coins may return non-json responses.
try
{
var errorResponse = JsonConvert.DeserializeObject<RpcErrorResponse>(error); // so let's try parsing the error response as json.
throw _rpcExceptionFactory.GetRpcErrorException(errorResponse); // if we can use the error json
}
catch (JsonException e) // if we can't parse the error response as json
{
throw _rpcExceptionFactory.GetRpcException(error, e); // then just use the error text.
}
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while reading json response.", exception);
}
}
}
catch (Exception exception)
{
throw _rpcExceptionFactory.GetRpcException("An unknown exception occured while trying to read the JSON response.", exception);
}
}
}
}
2 changes: 1 addition & 1 deletion src/CoiniumServ/Daemon/Exceptions/RpcException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RpcException(string message) :
{ }

public RpcException(Exception inner) :
base("Generic rpc exception", inner)
base(inner.Message, inner)
{ }

public RpcException(string message, Exception innerException) :
Expand Down
2 changes: 1 addition & 1 deletion src/CoiniumServ/Shares/ShareManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private bool SubmitBlock(IShare share)
// unlike BlockProcessor's detailed exception handling and decision making based on the error,
// here in share-manager we only one-shot submissions. If we get an error, basically we just don't care about the rest
// and flag the submission as failed.
_logger.Error("We thought a block was found but it was rejected by the coin daemon; [{0:l}] - reason; {1:l}", share.BlockHash.ToHexString(), e.Message);
_logger.Debug("We thought a block was found but it was rejected by the coin daemon; [{0:l}] - reason; {1:l}", share.BlockHash.ToHexString(), e.Message);
return false;
}
}
Expand Down

0 comments on commit 6379590

Please sign in to comment.