Updated test logic to report exceptions gracefully.

This commit is contained in:
Taloth Saldono 2014-07-10 00:11:57 +02:00 committed by Mark McDowall
parent 25abeb8c9c
commit f9586e6f77
8 changed files with 91 additions and 29 deletions

View File

@ -1,14 +1,30 @@
using NzbDrone.Common.Exceptions; using System;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Download.Clients namespace NzbDrone.Core.Download.Clients
{ {
public class DownloadClientException : NzbDroneException public class DownloadClientException : NzbDroneException
{ {
public DownloadClientException(string message, params object[] args) : base(message, args) public DownloadClientException(string message, params object[] args)
: base(string.Format(message, args))
{ {
} }
public DownloadClientException(string message) : base(message) public DownloadClientException(string message)
: base(message)
{
}
public DownloadClientException(string message, Exception innerException, params object[] args)
: base(string.Format(message, args), innerException)
{
}
public DownloadClientException(string message, Exception innerException)
: base(message, innerException)
{ {
} }
} }

View File

@ -11,6 +11,7 @@ using NzbDrone.Core.Configuration;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Nzbget namespace NzbDrone.Core.Download.Clients.Nzbget
{ {
@ -290,10 +291,8 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
} }
} }
public override ValidationResult Test() protected override void Test(List<ValidationFailure> failures)
{ {
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(TestConnection()); failures.AddIfNotNull(TestConnection());
failures.AddIfNotNull(TestCategory()); failures.AddIfNotNull(TestCategory());
@ -301,8 +300,6 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
{ {
failures.AddIfNotNull(TestFolder(Settings.TvCategoryLocalPath, "TvCategoryLocalPath")); failures.AddIfNotNull(TestFolder(Settings.TvCategoryLocalPath, "TvCategoryLocalPath"));
} }
return new ValidationResult(failures);
} }
private ValidationFailure TestConnection() private ValidationFailure TestConnection()
@ -313,6 +310,10 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
} }
catch (Exception ex) catch (Exception ex)
{ {
if (ex.Message.ContainsIgnoreCase("Authentication failed"))
{
return new ValidationFailure("Username", "Authentication failed");
}
_logger.ErrorException(ex.Message, ex); _logger.ErrorException(ex.Message, ex);
return new ValidationFailure("Host", "Unable to connect to NZBGet"); return new ValidationFailure("Host", "Unable to connect to NZBGet");
} }
@ -327,7 +328,11 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
if (!Settings.TvCategory.IsNullOrWhiteSpace() && !categories.Any(v => v.Name == Settings.TvCategory)) if (!Settings.TvCategory.IsNullOrWhiteSpace() && !categories.Any(v => v.Name == Settings.TvCategory))
{ {
return new ValidationFailure("TvCategory", "Category does not exist"); return new NzbDroneValidationFailure("TvCategory", "Category does not exist")
{
InfoLink = String.Format("http://{0}:{1}/", Settings.Host, Settings.Port),
DetailedDescription = "The Category your entered doesn't exist in NzbGet. Go to NzbGet to create it."
};
} }
return null; return null;

View File

@ -193,7 +193,12 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
{ {
if (response.ResponseStatus != ResponseStatus.Completed) if (response.ResponseStatus != ResponseStatus.Completed)
{ {
throw new DownloadClientException("Unable to connect to NzbGet, please check your settings"); throw new DownloadClientException("Unable to connect to NzbGet, please check your settings", response.ErrorException);
}
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
throw new DownloadClientException("Authentication failed for NzbGet, please check your settings", response.ErrorException);
} }
var result = Json.Deserialize<JsonError>(response.Content); var result = Json.Deserialize<JsonError>(response.Content);

View File

@ -95,13 +95,9 @@ namespace NzbDrone.Core.Download.Clients.Pneumatic
return status; return status;
} }
public override ValidationResult Test() protected override void Test(List<ValidationFailure> failures)
{ {
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(TestWrite(Settings.NzbFolder, "NzbFolder")); failures.AddIfNotNull(TestWrite(Settings.NzbFolder, "NzbFolder"));
return new ValidationResult(failures);
} }
private ValidationFailure TestWrite(String folder, String propertyName) private ValidationFailure TestWrite(String folder, String propertyName)

View File

@ -303,19 +303,16 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
return status; return status;
} }
public override ValidationResult Test() protected override void Test(List<ValidationFailure> failures)
{ {
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(TestConnection()); failures.AddIfNotNull(TestConnection());
failures.AddIfNotNull(TestAuthentication());
failures.AddIfNotNull(TestCategory()); failures.AddIfNotNull(TestCategory());
if (!Settings.TvCategoryLocalPath.IsNullOrWhiteSpace()) if (!Settings.TvCategoryLocalPath.IsNullOrWhiteSpace())
{ {
failures.AddIfNotNull(TestFolder(Settings.TvCategoryLocalPath, "TvCategoryLocalPath")); failures.AddIfNotNull(TestFolder(Settings.TvCategoryLocalPath, "TvCategoryLocalPath"));
} }
return new ValidationResult(failures);
} }
private ValidationFailure TestConnection() private ValidationFailure TestConnection()
@ -333,6 +330,28 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
return null; return null;
} }
private ValidationFailure TestAuthentication()
{
try
{
_proxy.GetConfig(Settings);
}
catch (Exception ex)
{
if (ex.Message.ContainsIgnoreCase("API Key Incorrect"))
{
return new ValidationFailure("APIKey", "API Key Incorrect");
}
if (ex.Message.ContainsIgnoreCase("API Key Required"))
{
return new ValidationFailure("APIKey", "API Key Required");
}
throw;
}
return null;
}
private ValidationFailure TestCategory() private ValidationFailure TestCategory()
{ {
var config = this._proxy.GetConfig(Settings); var config = this._proxy.GetConfig(Settings);

View File

@ -145,7 +145,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
{ {
if (response.ResponseStatus != ResponseStatus.Completed) if (response.ResponseStatus != ResponseStatus.Completed)
{ {
throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings"); throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings", response.ErrorException);
} }
SabnzbdJsonError result; SabnzbdJsonError result;

View File

@ -143,14 +143,10 @@ namespace NzbDrone.Core.Download.Clients.UsenetBlackhole
}; };
} }
public override ValidationResult Test() protected override void Test(List<ValidationFailure> failures)
{ {
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(TestFolder(Settings.NzbFolder, "NzbFolder")); failures.AddIfNotNull(TestFolder(Settings.NzbFolder, "NzbFolder"));
failures.AddIfNotNull(TestFolder(Settings.WatchFolder, "WatchFolder")); failures.AddIfNotNull(TestFolder(Settings.WatchFolder, "WatchFolder"));
return new ValidationResult(failures);
} }
} }
} }

View File

@ -11,6 +11,7 @@ using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NLog; using NLog;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download namespace NzbDrone.Core.Download
{ {
@ -39,7 +40,6 @@ namespace NzbDrone.Core.Download
} }
public ProviderDefinition Definition { get; set; } public ProviderDefinition Definition { get; set; }
public abstract ValidationResult Test();
protected TSettings Settings protected TSettings Settings
{ {
@ -101,11 +101,33 @@ namespace NzbDrone.Core.Download
} }
} }
public ValidationResult Test()
{
var failures = new List<ValidationFailure>();
try
{
Test(failures);
}
catch (Exception ex)
{
_logger.ErrorException("Test aborted due to exception", ex);
failures.Add(new ValidationFailure(string.Empty, "Test was aborted due to an error: " + ex.Message));
}
return new ValidationResult(failures);
}
protected abstract void Test(List<ValidationFailure> failures);
protected ValidationFailure TestFolder(String folder, String propertyName, Boolean mustBeWritable = true) protected ValidationFailure TestFolder(String folder, String propertyName, Boolean mustBeWritable = true)
{ {
if (!_diskProvider.FolderExists(folder)) if (!_diskProvider.FolderExists(folder))
{ {
return new ValidationFailure(propertyName, "Folder does not exist"); return new NzbDroneValidationFailure(propertyName, "Folder does not exist")
{
DetailedDescription = "The folder you specified does not exist or is inaccessible. Please verify the folder permissions for the user account that is used to execute NzbDrone."
};
} }
if (mustBeWritable) if (mustBeWritable)
@ -119,7 +141,10 @@ namespace NzbDrone.Core.Download
catch (Exception ex) catch (Exception ex)
{ {
_logger.ErrorException(ex.Message, ex); _logger.ErrorException(ex.Message, ex);
return new ValidationFailure(propertyName, "Unable to write to folder"); return new NzbDroneValidationFailure(propertyName, "Unable to write to folder")
{
DetailedDescription = "The folder you specified is not writable. Please verify the folder permissions for the user account that is used to execute NzbDrone."
};
} }
} }