From 1ee8568205506a567e0550309c26a8b432e9ac70 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 25 May 2026 23:51:57 -0400 Subject: [PATCH] Preserve JSON-RPC error data in .NET Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- dotnet/src/JsonRpc.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dotnet/src/JsonRpc.cs b/dotnet/src/JsonRpc.cs index df7170373..ff2fc7f46 100644 --- a/dotnet/src/JsonRpc.cs +++ b/dotnet/src/JsonRpc.cs @@ -440,7 +440,10 @@ private void HandleResponse(JsonElement message, JsonElement idProp) var errorCode = errorProp.TryGetProperty("code", out var codeProp) && codeProp.ValueKind == JsonValueKind.Number ? codeProp.GetInt32() : 0; - pending.TrySetException(new RemoteRpcException(errorMessage, errorCode)); + var errorData = errorProp.TryGetProperty("data", out var dataProp) + ? dataProp.Clone() + : (JsonElement?)null; + pending.TrySetException(new RemoteRpcException(errorMessage, errorCode, errorData)); } else if (message.TryGetProperty("result", out var resultProp)) { @@ -899,12 +902,14 @@ internal sealed class ConnectionLostException() : IOException("The JSON-RPC conn /// /// Thrown when the remote side returns a JSON-RPC error response. /// -internal sealed class RemoteRpcException(string message, int errorCode, Exception? innerException = null) : Exception(message, innerException) +internal sealed class RemoteRpcException(string message, int errorCode, JsonElement? errorData = null, Exception? innerException = null) : Exception(message, innerException) { /// JSON-RPC 2.0 reserved error code: requested method does not exist. public const int MethodNotFoundErrorCode = -32601; public int ErrorCode { get; } = errorCode; + + public JsonElement? ErrorData { get; } = errorData.HasValue ? errorData.Value.Clone() : null; } ///