|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using AustinHarris.JsonRpc; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +namespace AustinHarris.JsonRpcTestN |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// What an error handler receives in <c>error.data</c> when a method throws: the exception the method threw, |
| 15 | + /// with its own cause still attached. Only the wrappers the invocation machinery adds are stripped, a |
| 16 | + /// <see cref="TargetInvocationException"/> and an <see cref="AggregateException"/> holding one exception; an |
| 17 | + /// inner exception is never substituted for the outer one, and an inner <see cref="JsonRpcException"/> is not |
| 18 | + /// promoted. The same rule on the synchronous and the asynchronous path. |
| 19 | + /// </summary> |
| 20 | + [TestFixture] |
| 21 | + public sealed class ErrorHandlerExceptionTests |
| 22 | + { |
| 23 | + private string _session; |
| 24 | + private Exception _seen; |
| 25 | + |
| 26 | + [SetUp] |
| 27 | + public void SetUp() |
| 28 | + { |
| 29 | + _session = "error-handler-" + Guid.NewGuid().ToString("N"); |
| 30 | + _seen = null; |
| 31 | + Handler.GetSessionHandler(_session).SetErrorHandler((request, error) => { _seen = error.data as Exception; return error; }); |
| 32 | + } |
| 33 | + |
| 34 | + [TearDown] |
| 35 | + public void TearDown() => Handler.DestroySession(_session); |
| 36 | + |
| 37 | + private static string Request(string method, string id = "1") => "{\"jsonrpc\":\"2.0\",\"method\":\"" + method + "\"" + (id == null ? "" : ",\"id\":" + id) + "}"; |
| 38 | + |
| 39 | + private string Sync(string json) |
| 40 | + { |
| 41 | + var output = new ArrayBufferWriter<byte>(); |
| 42 | + JsonRpcProcessor.Process(_session, Encoding.UTF8.GetBytes(json).AsSpan(), output); |
| 43 | + return Encoding.UTF8.GetString(output.WrittenSpan); |
| 44 | + } |
| 45 | + |
| 46 | + private Task<string> Async(string json) => JsonRpcProcessor.ProcessAsync(_session, json); |
| 47 | + |
| 48 | + private static JObject Error(string json, int code) |
| 49 | + { |
| 50 | + var response = JObject.Parse(json); |
| 51 | + Assert.IsNotNull(response["error"], json); |
| 52 | + Assert.AreEqual(code, (int)response["error"]["code"], json); |
| 53 | + return response; |
| 54 | + } |
| 55 | + |
| 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> |
| 61 | + private void Bind(Func<Exception> thrower) |
| 62 | + { |
| 63 | + ServiceBinder.BindMethod(_session, "sync", new Func<int>(() => throw thrower())); |
| 64 | + ServiceBinder.BindMethod(_session, "hooked", new Func<int>(() => throw thrower())); |
| 65 | + 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())))); |
| 67 | + } |
| 68 | + |
| 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")] |
| 80 | + public async Task ThrownException_ReachesTheHandler_WithItsCause(string path) |
| 81 | + { |
| 82 | + Bind(() => new InvalidOperationException("outer", new IOException("cause"))); |
| 83 | + string response = await Run(path); |
| 84 | + var error = Error(response, -32603); |
| 85 | + Assert.AreEqual(JTokenType.Null, error["error"]["data"].Type, "redacted on the wire by default"); |
| 86 | + Assert.IsInstanceOf<InvalidOperationException>(_seen, "the handler gets the exception that was thrown, not its cause"); |
| 87 | + Assert.AreEqual("outer", _seen.Message); |
| 88 | + Assert.IsInstanceOf<IOException>(_seen.InnerException, "with its cause still attached"); |
| 89 | + } |
| 90 | + |
| 91 | + [TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")] |
| 92 | + public async Task InnerJsonRpcException_IsNotPromoted(string path) |
| 93 | + { |
| 94 | + Bind(() => new InvalidOperationException("outer", new JsonRpcException(-32040, "authored inside", "data"))); |
| 95 | + string response = await Run(path); |
| 96 | + Error(response, -32603); |
| 97 | + Assert.IsInstanceOf<InvalidOperationException>(_seen); |
| 98 | + Assert.AreEqual("outer", _seen.Message); |
| 99 | + } |
| 100 | + |
| 101 | + [TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")] |
| 102 | + public async Task AuthoredException_IsPassedAsThrown(string path) |
| 103 | + { |
| 104 | + Bind(() => new JsonRpcException(-32040, "authored", "ticket 42")); |
| 105 | + string response = await Run(path); |
| 106 | + var error = Error(response, -32040); |
| 107 | + Assert.AreEqual("authored", (string)error["error"]["message"]); |
| 108 | + Assert.AreEqual("ticket 42", (string)error["error"]["data"]); |
| 109 | + } |
| 110 | + |
| 111 | + [TestCase("sync")] [TestCase("hooked")] [TestCase("async")] [TestCase("valueTask")] |
| 112 | + public async Task TargetInvocationWrapper_IsStripped(string path) |
| 113 | + { |
| 114 | + Bind(() => new TargetInvocationException(new JsonRpcException(-32040, "authored", null))); |
| 115 | + Error(await Run(path), -32040); |
| 116 | + } |
| 117 | + |
| 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)] |
| 120 | + public async Task Aggregate_OnlyASingleInner_IsStripped(string path, bool several) |
| 121 | + { |
| 122 | + var authored = new JsonRpcException(-32040, "authored", null); |
| 123 | + Bind(() => several ? new AggregateException(authored, new Exception("second")) : new AggregateException(authored)); |
| 124 | + Error(await Run(path), several ? -32603 : -32040); |
| 125 | + if (several) Assert.IsInstanceOf<AggregateException>(_seen, "an aggregate of several failures is passed whole"); |
| 126 | + } |
| 127 | + |
| 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 | + |
| 137 | + [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")] |
| 149 | + public async Task WithDetailsOn_TheWireCarriesTheOuterExceptionAndItsCause(string path) |
| 150 | + { |
| 151 | + Bind(() => new InvalidOperationException("outer", new IOException("cause"))); |
| 152 | + Config.IncludeExceptionDetails = true; |
| 153 | + try |
| 154 | + { |
| 155 | + string response = await Run(path); |
| 156 | + var data = (JObject)Error(response, -32603)["error"]["data"]; |
| 157 | + Assert.AreEqual("outer", (string)data["Message"], response); |
| 158 | + Assert.AreEqual("cause", (string)data["InnerException"]["Message"], response); |
| 159 | + } |
| 160 | + finally |
| 161 | + { |
| 162 | + Config.IncludeExceptionDetails = false; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments