Skip to content

Commit

Permalink
add throwOnError param to RpcClient Send/SendAsync (#763)
Browse files Browse the repository at this point in the history
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 5, 2022
1 parent 14183a3 commit f84d63c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ static RpcRequest AsRpcRequest(string method, params JToken[] paraArgs)
Params = paraArgs
};
}
static RpcResponse AsRpcResponse(string content)

static RpcResponse AsRpcResponse(string content, bool throwOnError)
{
var response = RpcResponse.FromJson((JObject)JToken.Parse(content));
response.RawResponse = content;

if (response.Error != null)
if (response.Error != null && throwOnError)
{
throw new RpcException(response.Error.Code, response.Error.Message);
}
Expand All @@ -110,25 +111,25 @@ HttpRequestMessage AsHttpRequest(RpcRequest request)
};
}

public RpcResponse Send(RpcRequest request)
public RpcResponse Send(RpcRequest request, bool throwOnError = true)
{
if (disposedValue) throw new ObjectDisposedException(nameof(RpcClient));

using var requestMsg = AsHttpRequest(request);
using var responseMsg = httpClient.Send(requestMsg);
using var contentStream = responseMsg.Content.ReadAsStream();
using var contentReader = new StreamReader(contentStream);
return AsRpcResponse(contentReader.ReadToEnd());
return AsRpcResponse(contentReader.ReadToEnd(), throwOnError);
}

public async Task<RpcResponse> SendAsync(RpcRequest request)
public async Task<RpcResponse> SendAsync(RpcRequest request, bool throwOnError = true)
{
if (disposedValue) throw new ObjectDisposedException(nameof(RpcClient));

using var requestMsg = AsHttpRequest(request);
using var responseMsg = await httpClient.SendAsync(requestMsg).ConfigureAwait(false);
var content = await responseMsg.Content.ReadAsStringAsync();
return AsRpcResponse(content);
return AsRpcResponse(content, throwOnError);
}

public virtual JToken RpcSend(string method, params JToken[] paraArgs)
Expand Down
29 changes: 29 additions & 0 deletions tests/Neo.Network.RPC.Tests/UT_RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public async Task TestErrorResponse()
}
}

[TestMethod]
public async Task TestNoThrowErrorResponse()
{
var test = TestUtils.RpcTestCases.Find(p => p.Name == (nameof(rpc.SendRawTransactionAsync) + "error").ToLower());
handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
handlerMock.Protected()
// Setup the PROTECTED method to mock
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
// prepare the expected response of the mocked http call
.ReturnsAsync(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(test.Response.ToJson().ToString()),
})
.Verifiable();

var httpClient = new HttpClient(handlerMock.Object);
var client = new RpcClient(httpClient, new Uri("http://seed1.neo.org:10331"), null);
var response = await client.SendAsync(test.Request, false);

Assert.IsNull(response.Result);
Assert.IsNotNull(response.Error);
Assert.AreEqual(-500, response.Error.Code);
Assert.AreEqual("InsufficientFunds", response.Error.Message);
}

[TestMethod]
public void TestConstructorByUrlAndDispose()
{
Expand Down

0 comments on commit f84d63c

Please sign in to comment.