1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

Better error handling in the Deluge ConnectDaemon code.

This commit is contained in:
Taloth Saldono 2016-04-06 22:07:25 +02:00
parent 71ecc96c70
commit 9f34127565

View File

@ -177,7 +177,7 @@ protected TResult ProcessRequest<TResult>(DelugeSettings settings, string method
{
var requestBuilder = BuildRequest(settings);
var response = ProcessRequest<TResult>(requestBuilder, method, arguments);
var response = ExecuteRequest<TResult>(requestBuilder, method, arguments);
if (response.Error != null)
{
@ -186,7 +186,7 @@ protected TResult ProcessRequest<TResult>(DelugeSettings settings, string method
{
AuthenticateClient(requestBuilder, settings, true);
response = ProcessRequest<TResult>(requestBuilder, method, arguments);
response = ExecuteRequest<TResult>(requestBuilder, method, arguments);
if (response.Error == null)
{
@ -203,7 +203,7 @@ protected TResult ProcessRequest<TResult>(DelugeSettings settings, string method
return response.Result;
}
private JsonRpcResponse<TResult> ProcessRequest<TResult>(JsonRpcRequestBuilder requestBuilder, string method, params object[] arguments)
private JsonRpcResponse<TResult> ExecuteRequest<TResult>(JsonRpcRequestBuilder requestBuilder, string method, params object[] arguments)
{
var request = requestBuilder.Call(method, arguments).Build();
@ -235,6 +235,15 @@ private JsonRpcResponse<TResult> ProcessRequest<TResult>(JsonRpcRequestBuilder r
}
}
private void VerifyResponse<TResult>(JsonRpcResponse<TResult> response)
{
if (response.Error != null)
{
var error = response.Error.ToObject<DelugeError>();
throw new DelugeException(error.Message, error.Code);
}
}
private void AuthenticateClient(JsonRpcRequestBuilder requestBuilder, DelugeSettings settings, bool reauthenticate = false)
{
var authKey = string.Format("{0}:{1}", requestBuilder.BaseUrl, settings.Password);
@ -271,14 +280,16 @@ private void AuthenticateClient(JsonRpcRequestBuilder requestBuilder, DelugeSett
private void ConnectDaemon(JsonRpcRequestBuilder requestBuilder)
{
var resultConnected = ProcessRequest<bool>(requestBuilder, "web.connected");
var resultConnected = ExecuteRequest<bool>(requestBuilder, "web.connected");
VerifyResponse(resultConnected);
if (resultConnected.Result)
{
return;
}
var resultHosts = ProcessRequest<List<object[]>>(requestBuilder, "web.get_hosts");
var resultHosts = ExecuteRequest<List<object[]>>(requestBuilder, "web.get_hosts");
VerifyResponse(resultHosts);
if (resultHosts.Result != null)
{
@ -287,13 +298,14 @@ private void ConnectDaemon(JsonRpcRequestBuilder requestBuilder)
if (connection != null)
{
ProcessRequest<object>(requestBuilder, "web.connect", new object[] { connection[0] });
}
else
{
throw new DownloadClientException("Failed to connect to Deluge daemon.");
var resultConnect = ExecuteRequest<object>(requestBuilder, "web.connect", new object[] { connection[0] });
VerifyResponse(resultConnect);
return;
}
}
throw new DownloadClientException("Failed to connect to Deluge daemon.");
}
private DelugeTorrent[] GetTorrents(DelugeUpdateUIResult result)