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
1 changed files with 22 additions and 10 deletions

View File

@ -177,7 +177,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge
{
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 @@ namespace NzbDrone.Core.Download.Clients.Deluge
{
AuthenticateClient(requestBuilder, settings, true);
response = ProcessRequest<TResult>(requestBuilder, method, arguments);
response = ExecuteRequest<TResult>(requestBuilder, method, arguments);
if (response.Error == null)
{
@ -203,7 +203,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge
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 @@ namespace NzbDrone.Core.Download.Clients.Deluge
}
}
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 @@ namespace NzbDrone.Core.Download.Clients.Deluge
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 @@ namespace NzbDrone.Core.Download.Clients.Deluge
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)