From d76f88f759d046b07222f3b9bb1fa99d471b4b36 Mon Sep 17 00:00:00 2001 From: bonesoul Date: Mon, 27 Oct 2014 13:44:54 +0200 Subject: [PATCH] Added more exception handlers to DaemonBase.cs. --- src/CoiniumServ/Daemon/DaemonBase.cs | 78 +++++++++++++------ .../Daemon/Exceptions/RpcException.cs | 2 +- src/CoiniumServ/Shares/ShareManager.cs | 2 +- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/CoiniumServ/Daemon/DaemonBase.cs b/src/CoiniumServ/Daemon/DaemonBase.cs index 89685ff6c..69e8ebaf7 100644 --- a/src/CoiniumServ/Daemon/DaemonBase.cs +++ b/src/CoiniumServ/Daemon/DaemonBase.cs @@ -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; @@ -173,6 +177,10 @@ private DaemonResponse GetRpcResponse(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); + } } /// @@ -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) @@ -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(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(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); - } } } } diff --git a/src/CoiniumServ/Daemon/Exceptions/RpcException.cs b/src/CoiniumServ/Daemon/Exceptions/RpcException.cs index bc72e5fcf..bb642c84e 100644 --- a/src/CoiniumServ/Daemon/Exceptions/RpcException.cs +++ b/src/CoiniumServ/Daemon/Exceptions/RpcException.cs @@ -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) : diff --git a/src/CoiniumServ/Shares/ShareManager.cs b/src/CoiniumServ/Shares/ShareManager.cs index 3e781e94a..0095ba30e 100644 --- a/src/CoiniumServ/Shares/ShareManager.cs +++ b/src/CoiniumServ/Shares/ShareManager.cs @@ -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; } }