/// <summary> /// Parses and returns the result of the rpc response as the type specified. /// Otherwise throws a parsing exception /// </summary> /// <typeparam name="T">Type of object to parse the response as</typeparam> /// <param name="response">Rpc response object</param> /// <param name="returnDefaultIfNull"> /// Returns the type's default value if the result is null. Otherwise throws parsing /// exception /// </param> /// <param name="settings"></param> /// <returns>Result of response as type specified</returns> public static T GetResult <T>(this RpcResponseMessage response, bool returnDefaultIfNull = true, JsonSerializerSettings settings = null) { if (response.Result == null) { if (!returnDefaultIfNull && default(T) != null) { throw new Exception("Unable to convert the result (null) to type " + typeof(T)); } return(default(T)); } try { if (settings == null) { return(response.Result.ToObject <T>()); } var jsonSerializer = JsonSerializer.Create(settings); return(response.Result.ToObject <T>(jsonSerializer)); } catch (Exception ex) { throw new Exception("Unable to convert the result to type " + typeof(T), ex); } }
private static void HandleRpcError(RpcResponseMessage response) { if (response.HasError) { throw new RpcResponseException(new RpcError(response.Error.Code, response.Error.Message, response.Error.Data)); } }