Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add throwOnError param to RpcClient Send/SendAsync #763

Merged
merged 4 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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