using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; namespace GumartinM.JsonRPC4NET { /// /// Exception resolver. /// public class ExceptionResolver { /// /// Resolves the exception. /// /// The exception. /// Error token. public Exception ResolveException(JToken errorToken) { JObject errorData = (JObject)errorToken; IDictionary errorTokens = errorData; if (!errorTokens.ContainsKey("data")) { return CreateJsonRpcClientException(errorToken); } JToken dataToken = errorToken["data"]; JObject data = (JObject)dataToken; errorTokens = data; if (!errorTokens.ContainsKey("exceptionTypeName")) { return CreateJsonRpcClientException(errorToken); } string exceptionTypeName = data["exceptionTypeName"].Value(); string message = data.Value("message"); Exception endException = CreateException(exceptionTypeName, message); return endException; } /// /// Creates the json rpc client exception. /// /// The json rpc client exception. /// Error token. private JsonRpcClientException CreateJsonRpcClientException(JToken errorToken) { return new JsonRpcClientException( errorToken.Value("code") ?? 0, errorToken.Value("message"), errorToken["data"]); } /// /// Creates the exception. /// /// The exception. /// Exception type name. /// Message. private Exception CreateException(string exceptionTypeName, string message) { return new Exception("Remote exception: " + exceptionTypeName + " Remote message: " + message); } } }