Skip to content

Commit e10aec6

Browse files
committed
AUS-1015: State the unwrap rule by type and cover the hook, ValueTask, notification and conversion paths
1 parent f27946f commit e10aec6

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

‎AustinHarris.JsonRpcTestN/ErrorHandlerExceptionTests.cs‎

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void SetUp()
3434
[TearDown]
3535
public void TearDown() => Handler.DestroySession(_session);
3636

37-
private static string Request(string method, string id = "1") => "{\"jsonrpc\":\"2.0\",\"method\":\"" + method + "\",\"id\":" + id + "}";
37+
private static string Request(string method, string id = "1") => "{\"jsonrpc\":\"2.0\",\"method\":\"" + method + "\"" + (id == null ? "" : ",\"id\":" + id) + "}";
3838

3939
private string Sync(string json)
4040
{
@@ -53,71 +53,106 @@ private static JObject Error(string json, int code)
5353
return response;
5454
}
5555

56-
/// <summary>Runs <paramref name="thrower"/> as a synchronous method, then as a Task method that has already suspended.</summary>
56+
/// <summary>
57+
/// Binds <paramref name="thrower"/> as a synchronous method, a Task method that has already suspended, and a
58+
/// ValueTask method that faults synchronously; "hooked" runs the synchronous method with a pre-process handler
59+
/// installed, which takes the hook path through the request objects.
60+
/// </summary>
5761
private void Bind(Func<Exception> thrower)
5862
{
5963
ServiceBinder.BindMethod(_session, "sync", new Func<int>(() => throw thrower()));
64+
ServiceBinder.BindMethod(_session, "hooked", new Func<int>(() => throw thrower()));
6065
ServiceBinder.BindMethod(_session, "async", new Func<Task<int>>(async () => { await Task.Yield(); throw thrower(); }));
66+
ServiceBinder.BindMethod(_session, "valueTask", new Func<ValueTask<int>>(() => new ValueTask<int>(Task.FromException<int>(thrower()))));
6167
}
6268

63-
[TestCase("sync")] [TestCase("async")]
69+
private async Task<string> Run(string path, string id = "1")
70+
{
71+
if (path == "hooked")
72+
{
73+
Config.SetPreProcessHandler(_session, (request, context) => null);
74+
return Sync(Request(path, id));
75+
}
76+
return path == "sync" ? Sync(Request(path, id)) : await Async(Request(path, id));
77+
}
78+
79+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
6480
public async Task ThrownException_ReachesTheHandler_WithItsCause(string path)
6581
{
6682
Bind(() => new InvalidOperationException("outer", new IOException("cause")));
67-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
83+
string response = await Run(path);
6884
var error = Error(response, -32603);
6985
Assert.AreEqual(JTokenType.Null, error["error"]["data"].Type, "redacted on the wire by default");
7086
Assert.IsInstanceOf<InvalidOperationException>(_seen, "the handler gets the exception that was thrown, not its cause");
7187
Assert.AreEqual("outer", _seen.Message);
7288
Assert.IsInstanceOf<IOException>(_seen.InnerException, "with its cause still attached");
7389
}
7490

75-
[TestCase("sync")] [TestCase("async")]
91+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
7692
public async Task InnerJsonRpcException_IsNotPromoted(string path)
7793
{
7894
Bind(() => new InvalidOperationException("outer", new JsonRpcException(-32040, "authored inside", "data")));
79-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
95+
string response = await Run(path);
8096
Error(response, -32603);
8197
Assert.IsInstanceOf<InvalidOperationException>(_seen);
8298
Assert.AreEqual("outer", _seen.Message);
8399
}
84100

85-
[TestCase("sync")] [TestCase("async")]
101+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
86102
public async Task AuthoredException_IsPassedAsThrown(string path)
87103
{
88104
Bind(() => new JsonRpcException(-32040, "authored", "ticket 42"));
89-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
105+
string response = await Run(path);
90106
var error = Error(response, -32040);
91107
Assert.AreEqual("authored", (string)error["error"]["message"]);
92108
Assert.AreEqual("ticket 42", (string)error["error"]["data"]);
93109
}
94110

95-
[TestCase("sync")] [TestCase("async")]
111+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
96112
public async Task TargetInvocationWrapper_IsStripped(string path)
97113
{
98114
Bind(() => new TargetInvocationException(new JsonRpcException(-32040, "authored", null)));
99-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
100-
Error(response, -32040);
115+
Error(await Run(path), -32040);
101116
}
102117

103-
[TestCase("sync", false)] [TestCase("sync", true)] [TestCase("async", false)] [TestCase("async", true)]
118+
[TestCase("sync", false)] [TestCase("sync", true)] [TestCase("hooked", false)] [TestCase("hooked", true)]
119+
[TestCase("async", false)] [TestCase("async", true)] [TestCase("valueTask", false)] [TestCase("valueTask", true)]
104120
public async Task Aggregate_OnlyASingleInner_IsStripped(string path, bool several)
105121
{
106122
var authored = new JsonRpcException(-32040, "authored", null);
107123
Bind(() => several ? new AggregateException(authored, new Exception("second")) : new AggregateException(authored));
108-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
109-
Error(response, several ? -32603 : -32040);
124+
Error(await Run(path), several ? -32603 : -32040);
110125
if (several) Assert.IsInstanceOf<AggregateException>(_seen, "an aggregate of several failures is passed whole");
111126
}
112127

128+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
129+
public async Task Notification_StillReachesTheHandler(string path)
130+
{
131+
Bind(() => new InvalidOperationException("outer", new IOException("cause")));
132+
Assert.AreEqual("", await Run(path, id: null), "a notification produces no response");
133+
Assert.IsInstanceOf<InvalidOperationException>(_seen, "but the handler still sees what was thrown");
134+
Assert.IsInstanceOf<IOException>(_seen.InnerException);
135+
}
136+
113137
[TestCase("sync")] [TestCase("async")]
138+
public async Task ConversionFailure_StaysInvalidParams(string path)
139+
{
140+
ServiceBinder.BindMethod(_session, "sync", new Func<int, int>(n => n));
141+
ServiceBinder.BindMethod(_session, "async", new Func<int, Task<int>>(async n => { await Task.Yield(); return n; }));
142+
string json = "{\"jsonrpc\":\"2.0\",\"method\":\"" + path + "\",\"params\":[\"many\"],\"id\":1}";
143+
string response = path == "sync" ? Sync(json) : await Async(json);
144+
var error = Error(response, -32602);
145+
Assert.AreEqual("conversion", (string)error["error"]["data"]["reason"], response);
146+
}
147+
148+
[TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")]
114149
public async Task WithDetailsOn_TheWireCarriesTheOuterExceptionAndItsCause(string path)
115150
{
116151
Bind(() => new InvalidOperationException("outer", new IOException("cause")));
117152
Config.IncludeExceptionDetails = true;
118153
try
119154
{
120-
string response = path == "sync" ? Sync(Request(path)) : await Async(Request(path));
155+
string response = await Run(path);
121156
var data = (JObject)Error(response, -32603)["error"]["data"];
122157
Assert.AreEqual("outer", (string)data["Message"], response);
123158
Assert.AreEqual("cause", (string)data["InnerException"]["Message"], response);

‎Json-Rpc/Handler.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,8 @@ private static object SafeIdValue(JsonRpcRequestReader reader)
915915

916916
/// <summary>
917917
/// Maps what an invocation threw to the error the handler sees: an authored <see cref="JsonRpcException"/> is
918-
/// passed as it is, anything else becomes -32603 with the thrown exception as <c>data</c>. Only the wrappers
919-
/// the invocation machinery adds are stripped first (<see cref="UnwrapInvocation"/>); the exception's own
918+
/// passed as it is, anything else becomes -32603 with the thrown exception as <c>data</c>. Only the two
919+
/// wrapper types are stripped first (<see cref="UnwrapInvocation"/>); the exception's own
920920
/// <see cref="Exception.InnerException"/> is never substituted for it, so an error handler always receives
921921
/// the object the method threw, with its cause still attached.
922922
/// </summary>
@@ -928,10 +928,11 @@ private JsonRpcException MapException(JsonRequest request, Exception ex)
928928
}
929929

930930
/// <summary>
931-
/// Strips the wrappers that reflection and task machinery put around what a method threw: a
931+
/// Strips the two wrapper types that reflection and task machinery put around what a method threw: a
932932
/// <see cref="TargetInvocationException"/>, and an <see cref="AggregateException"/> holding exactly one
933-
/// exception (a faulted task observed through <c>Result</c> or <c>Wait</c>). An aggregate of several
934-
/// failures is kept whole, since no single one of them is "the" error. Nothing else is unwrapped.
933+
/// exception (a faulted task observed through <c>Result</c> or <c>Wait</c>). The decision is by type alone,
934+
/// so a method that throws one of these itself is unwrapped the same way. An aggregate of several failures
935+
/// is kept whole, since no single one of them is "the" error. Nothing else is unwrapped.
935936
/// </summary>
936937
private static Exception UnwrapInvocation(Exception ex)
937938
{

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ Set `Config.IncludeExceptionDetails = true` only for trusted development clients
342342

343343
A `JsonRpcException` thrown by the application, or returned by an error handler, keeps the `data` it was given; that data is authored, not redacted.
344344

345-
The exception a handler receives is the one the method threw, with its own `InnerException` still attached; the library never substitutes the cause for the error. Only two wrappers added by the invocation machinery are stripped first: a `TargetInvocationException` from reflection and an `AggregateException` holding exactly one exception (a faulted task observed through `Result` or `Wait`). An aggregate of several exceptions is passed whole. A `JsonRpcException` found only inside another exception's `InnerException` is not promoted: the outer exception is an internal error like any other.
345+
The exception a handler receives is the one the method threw, with its own `InnerException` still attached; the library never substitutes the cause for the error. Only two wrapper types are stripped first, by type and wherever they came from: a `TargetInvocationException` (what reflection adds) and an `AggregateException` holding exactly one exception (what a faulted task observed through `Result` or `Wait` adds). An aggregate of several exceptions is passed whole. A `JsonRpcException` found only inside another exception's `InnerException` is not promoted: the outer exception is an internal error like any other.
346346

347347
### Handlers
348348

0 commit comments

Comments
 (0)