Skip to content

Commit

Permalink
Handle error message returned from the node (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangershony authored Aug 26, 2022
1 parent 0a54cdf commit 11e5b96
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/Blockcore.Indexer.Core/Client/BitcoinClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ private async Task<T> CheckResponseOkAsync<T>(HttpResponseMessage response)

JsonRpcResponse<T> ret = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(jsonResult);

if (ret?.Error != null)
{
HandleError(jsonResult, response);
}

return ret == null ? default(T) : ret.Result;
}
}
Expand Down Expand Up @@ -628,29 +633,21 @@ private T CheckResponseOk<T>(HttpResponseMessage response)
{
using (var jsonStreamReader = new StreamReader(jsonStream))
{
string jsonResult = jsonStreamReader.ReadToEndAsync().Result;

if (response.StatusCode != HttpStatusCode.OK)
{
JsonRpcResponse<T> errRet = null;
string res = jsonStreamReader.ReadToEndAsync().Result;

try
{
errRet = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(res);
}
catch
{
throw CreateException(response, 0, res);
}

int code = errRet != null && errRet.Error != null ? errRet.Error.Code : 0;
string msg = errRet != null && errRet.Error != null ? errRet.Error.Message : "Error";

throw CreateException(response, code, msg);
HandleError(jsonResult, response);
}

JsonRpcResponse<T> ret = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(jsonStreamReader.ReadToEndAsync().Result);
JsonRpcResponse<T> ret = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(jsonResult);

if (ret?.Error != null)
{
HandleError(jsonResult, response);
}

return ret.Result;
return ret == null ? default(T) : ret.Result;
}
}
}
Expand Down

0 comments on commit 11e5b96

Please sign in to comment.