Skip to content

Commit 183cc44

Browse files
stephentoubCopilot
andauthored
Preserve JSON-RPC error data in .NET (#1425)
* Preserve JSON-RPC error data in .NET Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address JSON-RPC error data review feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix JsonRpc test nullability warning Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address JsonRpc test nullability feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Revert JsonRpcTests null-forgiving change Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Remove JsonRpcTests changes from PR Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3dd7b01 commit 183cc44

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

dotnet/src/JsonRpc.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ private void HandleResponse(JsonElement message, JsonElement idProp)
440440
var errorCode = errorProp.TryGetProperty("code", out var codeProp) && codeProp.ValueKind == JsonValueKind.Number
441441
? codeProp.GetInt32()
442442
: 0;
443-
pending.TrySetException(new RemoteRpcException(errorMessage, errorCode));
443+
var errorData = errorProp.TryGetProperty("data", out var dataProp)
444+
? dataProp
445+
: (JsonElement?)null;
446+
pending.TrySetException(new RemoteRpcException(errorMessage, errorCode, errorData));
444447
}
445448
else if (message.TryGetProperty("result", out var resultProp))
446449
{
@@ -899,12 +902,14 @@ internal sealed class ConnectionLostException() : IOException("The JSON-RPC conn
899902
/// <summary>
900903
/// Thrown when the remote side returns a JSON-RPC error response.
901904
/// </summary>
902-
internal sealed class RemoteRpcException(string message, int errorCode, Exception? innerException = null) : Exception(message, innerException)
905+
internal sealed class RemoteRpcException(string message, int errorCode, JsonElement? errorData = null, Exception? innerException = null) : Exception(message, innerException)
903906
{
904907
/// <summary>JSON-RPC 2.0 reserved error code: requested method does not exist.</summary>
905908
public const int MethodNotFoundErrorCode = -32601;
906909

907910
public int ErrorCode { get; } = errorCode;
911+
912+
public JsonElement? ErrorData { get; } = errorData.HasValue ? errorData.Value.Clone() : null;
908913
}
909914

910915
/// <summary>

0 commit comments

Comments
 (0)