-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathJsonRpcError.cs
124 lines (104 loc) · 3.5 KB
/
JsonRpcError.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Meadow.JsonRpc
{
public class JsonRpcError
{
[JsonProperty("code")]
public long Code { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("data")]
public object Data { get; set; }
/// <summary>
/// Properties not deserialized any members
/// </summary>
[JsonExtensionData]
public IDictionary<string, JToken> ExtraFields { get; set; }
public JsonRpcErrorException ToException() => new JsonRpcErrorException(this);
}
public class JsonRpcErrorException : Exception
{
public long Code => Error.Code;
public readonly JsonRpcError Error;
public readonly string ServerData;
public JsonRpcErrorException(JsonRpcError err) : base(err.Message)
{
Error = err;
if (err.Data is JObject dataDict)
{
ServerData = dataDict.ToString(Formatting.Indented);
// Populate this exception Data dictionary with entries from the RPC data object.
foreach (var entry in dataDict)
{
var entryVal = entry.Value.ToString();
Data[entry.Key] = entryVal;
}
}
else
{
ServerData = err.Data?.ToString() ?? string.Empty;
}
}
public JsonRpcErrorException(JsonRpcErrorCode errorCode, string message, Exception ex) : base(message, ex)
{
Error = new JsonRpcError
{
Code = (long)errorCode,
Message = message
};
var dataDict = new Dictionary<string, object>();
dataDict["exception"] = ex.ToString();
// Populate the RPC data object with entries from the exception's Data dictionary.
foreach (var item in ex.Data.Keys)
{
var dataVal = ex.Data[item];
if (dataVal != null)
{
dataDict[item.ToString()] = dataVal.ToString();
}
}
Error.Data = dataDict;
}
public JsonRpcErrorException(JsonRpcErrorCode errorCode, string message) : base(message)
{
Error = new JsonRpcError
{
Code = (long)errorCode,
Message = message
};
}
public override string ToString()
{
return $"{base.ToString()}{Environment.NewLine}ServerData:{Environment.NewLine}{ServerData}";
}
}
public enum JsonRpcErrorCode
{
/// <summary>
/// Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
/// </summary>
ParseError = -32700,
/// <summary>
/// The JSON sent is not a valid Request object.
/// </summary>
InvalidRequest = -32600,
/// <summary>
/// The method does not exist / is not available.
/// </summary>
MethodNotFound = -32601,
/// <summary>
/// Invalid method parameter(s).
/// </summary>
InvalidParams = -32602,
/// <summary>
/// Internal JSON-RPC error.
/// </summary>
InternalError = -32603,
ServerErrorStart = -32000,
ServerErrorEnd = -32099
}
}