From 6af98f9e96ba0614a2f7bbce2cf60ac4b4130018 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Mon, 17 Oct 2011 12:23:34 -0700 Subject: [PATCH] more better exception handling. --- NzbDrone.Core.Test/EpisodeProviderTest.cs | 22 ++++++++++----- .../Instrumentation/ExceptioneerTarget.cs | 4 ++- NzbDrone.Core/Providers/EpisodeProvider.cs | 4 ++- .../Providers/PostDownloadProvider.cs | 28 +++++++++++-------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/NzbDrone.Core.Test/EpisodeProviderTest.cs b/NzbDrone.Core.Test/EpisodeProviderTest.cs index 5a15aa203..2ac97720b 100644 --- a/NzbDrone.Core.Test/EpisodeProviderTest.cs +++ b/NzbDrone.Core.Test/EpisodeProviderTest.cs @@ -62,7 +62,7 @@ public void GetEpisodes_by_season_episode_exists() db.Insert(fakeSeries); db.Insert(fakeEpisodes); - + //Act var episode = mocker.Resolve().GetEpisode(fakeSeries.SeriesId, 2, 1); @@ -452,8 +452,8 @@ public void should_try_to_get_existing_episode_using_tvdbid_first() .Build(); var fakeSeries = Builder.CreateNew().With(c => c.SeriesId = seriesId).Build(); - var fakeEpisodeList = new List { new Episode { TvDbEpisodeId = 99, SeasonNumber = 10, EpisodeNumber = 10, Series = fakeSeries} }; - + var fakeEpisodeList = new List { new Episode { TvDbEpisodeId = 99, SeasonNumber = 10, EpisodeNumber = 10, Series = fakeSeries } }; + var mocker = new AutoMoqer(); mocker.GetMock() .Setup(d => d.Fetch(It.IsAny(), It.IsAny())) @@ -511,7 +511,7 @@ public void should_try_to_get_existing_episode_using_tvdbid_first_then_season_ep //Assert mocker.VerifyAllMocks(); - mocker.GetMock().Verify(c => c.UpdateMany(new List{localEpisode}), Times.Once()); + mocker.GetMock().Verify(c => c.UpdateMany(new List { localEpisode }), Times.Once()); } [Test] @@ -527,7 +527,7 @@ public void existing_episodes_keep_their_episodeId_file_id() var currentEpisodes = new List(); foreach (var tvDbEpisode in tvdbSeries.Episodes) { - currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, EpisodeId = 99, EpisodeFileId = 69, Ignored = true, Series = fakeSeries}); + currentEpisodes.Add(new Episode { TvDbEpisodeId = tvDbEpisode.Id, EpisodeId = 99, EpisodeFileId = 69, Ignored = true, Series = fakeSeries }); } var mocker = new AutoMoqer(); @@ -544,7 +544,7 @@ public void existing_episodes_keep_their_episodeId_file_id() mocker.GetMock() .Setup(c => c.UpdateMany(It.IsAny>())) - .Callback>(ep => updatedEpisodes =ep.ToList()); + .Callback>(ep => updatedEpisodes = ep.ToList()); //Act mocker.Resolve().RefreshEpisodeInfo(fakeSeries); @@ -1496,11 +1496,19 @@ public void SetPostDownloadStatus(string folderName, PostDownloadStatusType post mocker.GetMock().Setup(s => s.FindSeries("officeus")).Returns(fakeSeries); //Act - mocker.Resolve().SetPostDownloadStatus(fakeEpisodes.Select(e => e.EpisodeId), postDownloadStatus); + mocker.Resolve().SetPostDownloadStatus(fakeEpisodes.Select(e => e.EpisodeId).ToList(), postDownloadStatus); //Assert var result = db.Fetch(); result.Where(e => e.PostDownloadStatus == postDownloadStatus).Count().Should().Be(episodeCount); } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void SetPostDownloadStatus_should_throw_if_episode_list_is_empty() + { + var mocker = new AutoMoqer(); + mocker.Resolve().SetPostDownloadStatus(new List(), PostDownloadStatusType.Failed); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs b/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs index 3805eda37..1ab22074a 100644 --- a/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs +++ b/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs @@ -13,9 +13,11 @@ protected override void Write(LogEventInfo logEvent) { if (logEvent == null || logEvent.Exception == null) return; if (Debugger.IsAttached || Process.GetCurrentProcess().ProcessName.Contains("JetBrains")) return; - + Logger.Trace("Sending Exception to Exceptioneer. {0}", Process.GetCurrentProcess().ProcessName); + logEvent.Exception.Data.Add("Message", logEvent.Message); + new Client { ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265", diff --git a/NzbDrone.Core/Providers/EpisodeProvider.cs b/NzbDrone.Core/Providers/EpisodeProvider.cs index d8ce7c1dc..c8b841fd2 100644 --- a/NzbDrone.Core/Providers/EpisodeProvider.cs +++ b/NzbDrone.Core/Providers/EpisodeProvider.cs @@ -397,8 +397,10 @@ public virtual void DeleteInvalidEpisodes(Series series, TvdbSeries tvDbSeriesIn Logger.Trace("Finished deleting invalid episodes for {0}", series.SeriesId); } - public virtual void SetPostDownloadStatus(IEnumerable episodeIds, PostDownloadStatusType postDownloadStatus) + public virtual void SetPostDownloadStatus(List episodeIds, PostDownloadStatusType postDownloadStatus) { + if (episodeIds.Count == 0) throw new ArgumentException("episodeIds should contain one or more episode ids."); + var episodeIdString = String.Join(", ", episodeIds); var episodeIdQuery = String.Format(@"UPDATE Episodes SET PostDownloadStatus = {0} diff --git a/NzbDrone.Core/Providers/PostDownloadProvider.cs b/NzbDrone.Core/Providers/PostDownloadProvider.cs index 3d964dd6f..b17db66f9 100644 --- a/NzbDrone.Core/Providers/PostDownloadProvider.cs +++ b/NzbDrone.Core/Providers/PostDownloadProvider.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using NLog; using Ninject; using NzbDrone.Core.Model; @@ -23,7 +22,7 @@ public class PostDownloadProvider private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly List InfoList = new List(); - + [Inject] public PostDownloadProvider(ConfigProvider configProvider, DiskProvider diskProvider, DiskScanProvider diskScanProvider, SeriesProvider seriesProvider, @@ -38,7 +37,7 @@ public PostDownloadProvider(ConfigProvider configProvider, DiskProvider diskProv PostDownloadProvider() { - + } public virtual void ScanDropFolder(ProgressNotification notification) @@ -85,7 +84,7 @@ public virtual void ProcessDropFolder(string dropFolder) if (subfolderInfo.Name.StartsWith("_NzbDrone_")) { if (subfolderInfo.Name.StartsWith("_NzbDrone_InvalidSeries_")) - ReProcessDownload(new PostDownloadInfoModel{ Name = subfolderInfo.FullName, Status = PostDownloadStatusType.InvalidSeries }); + ReProcessDownload(new PostDownloadInfoModel { Name = subfolderInfo.FullName, Status = PostDownloadStatusType.InvalidSeries }); else if (subfolderInfo.Name.StartsWith("_NzbDrone_ParseError_")) ReProcessDownload(new PostDownloadInfoModel { Name = subfolderInfo.FullName, Status = PostDownloadStatusType.ParseError }); @@ -169,26 +168,31 @@ public virtual void ProcessFailedOrUnpackingDownload(DirectoryInfo directoryInfo InfoList.Remove(model); return; } - + //Add to InfoList for possible later processing - InfoList.Add(new PostDownloadInfoModel{ Name = directoryInfo.FullName, - Added = DateTime.Now, - Status = postDownloadStatus - }); + InfoList.Add(new PostDownloadInfoModel + { + Name = directoryInfo.FullName, + Added = DateTime.Now, + Status = postDownloadStatus + }); //Remove the first 8 characters of the folder name (removes _UNPACK_ or _FAILED_) before processing var parseResult = Parser.ParseTitle(directoryInfo.Name.Substring(8)); parseResult.Series = _seriesProvider.FindSeries(parseResult.CleanTitle); - var episodeIds = new List(); + List episodeIds; if (parseResult.EpisodeNumbers.Count == 0 && parseResult.FullSeason) + { episodeIds = _episodeProvider.GetEpisodesBySeason(parseResult.Series.SeriesId, parseResult.SeasonNumber) - .Select(e => e.EpisodeId).ToList(); - + .Select(e => e.EpisodeId).ToList(); + } else + { episodeIds = _episodeProvider.GetEpisodesByParseResult(parseResult).Select(e => e.EpisodeId).ToList(); + } _episodeProvider.SetPostDownloadStatus(episodeIds, postDownloadStatus); }