From a44be4d902ab1ba7db35c77c938337cfa6ace97f Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Fri, 3 Oct 2014 10:50:43 -0700 Subject: [PATCH] log newsnab 429 errors as warn instead of exceptions --- .../Http/HttpClientFixture.cs | 7 ++-- .../Exceptron/ExceptionExtentions.cs | 31 +++++++++++++++ .../Exceptron/ExceptronClient.cs | 28 -------------- src/NzbDrone.Common/Http/HttpException.cs | 2 +- src/NzbDrone.Common/NzbDrone.Common.csproj | 1 + src/NzbDrone.Core/Indexers/HttpIndexerBase.cs | 9 +++++ src/NzbDrone.Core/Indexers/Newznab/Newznab.cs | 3 -- .../MediaFiles/UpdateEpisodeFileService.cs | 38 ++++++++++--------- 8 files changed, 66 insertions(+), 53 deletions(-) create mode 100644 src/NzbDrone.Common/Exceptron/ExceptionExtentions.cs diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 02fde70da..b4e78b5ef 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -50,13 +50,14 @@ namespace NzbDrone.Common.Test.Http [TestCase(HttpStatusCode.InternalServerError)] [TestCase(HttpStatusCode.ServiceUnavailable)] [TestCase(HttpStatusCode.BadGateway)] - public void should_throw_on_unsuccessful_status_codes(HttpStatusCode statusCode) + [TestCase(429)] + public void should_throw_on_unsuccessful_status_codes(int statusCode) { - var request = new HttpRequest("http://eu.httpbin.org/status/" + (int)statusCode); + var request = new HttpRequest("http://eu.httpbin.org/status/" + statusCode); var exception = Assert.Throws(() => Subject.Get(request)); - exception.Response.StatusCode.Should().Be(statusCode); + ((int)exception.Response.StatusCode).Should().Be(statusCode); ExceptionVerification.IgnoreWarns(); } diff --git a/src/NzbDrone.Common/Exceptron/ExceptionExtentions.cs b/src/NzbDrone.Common/Exceptron/ExceptionExtentions.cs new file mode 100644 index 000000000..945ceb0d0 --- /dev/null +++ b/src/NzbDrone.Common/Exceptron/ExceptionExtentions.cs @@ -0,0 +1,31 @@ +using System; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Common.Exceptron +{ + public static class ExceptionExtentions + { + private const string IGNORE_FLAG = "exceptron_ignore"; + + public static Exception ExceptronIgnoreOnMono(this Exception exception) + { + if (OsInfo.IsMono) + { + exception.ExceptronIgnore(); + } + + return exception; + } + + public static Exception ExceptronIgnore(this Exception exception) + { + exception.Data.Add(IGNORE_FLAG, true); + return exception; + } + + public static bool ExceptronShouldIgnore(this Exception exception) + { + return exception.Data.Contains(IGNORE_FLAG); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Common/Exceptron/ExceptronClient.cs b/src/NzbDrone.Common/Exceptron/ExceptronClient.cs index a84c64b00..5dca71a4b 100644 --- a/src/NzbDrone.Common/Exceptron/ExceptronClient.cs +++ b/src/NzbDrone.Common/Exceptron/ExceptronClient.cs @@ -3,39 +3,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Threading; -using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Exceptron.Configuration; using NzbDrone.Common.Exceptron.Message; namespace NzbDrone.Common.Exceptron { - - public static class ExceptionExtentions - { - private const string IgnoreFlag = "exceptron_ignore"; - - public static Exception ExceptronIgnoreOnMono(this Exception exception) - { - if (OsInfo.IsMono) - { - exception.ExceptronIgnore(); - } - - return exception; - } - - public static Exception ExceptronIgnore(this Exception exception) - { - exception.Data.Add(IgnoreFlag, true); - return exception; - } - - public static bool ExceptronShouldIgnore(this Exception exception) - { - return exception.Data.Contains(IgnoreFlag); - } - } - public class ExceptronClient : IExceptronClient { internal IRestClient RestClient { private get; set; } diff --git a/src/NzbDrone.Common/Http/HttpException.cs b/src/NzbDrone.Common/Http/HttpException.cs index 3c8e38249..626d17774 100644 --- a/src/NzbDrone.Common/Http/HttpException.cs +++ b/src/NzbDrone.Common/Http/HttpException.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Common.Http public HttpResponse Response { get; private set; } public HttpException(HttpRequest request, HttpResponse response) - : base(string.Format("HTTP request failed: [{0}] [{1}] at [{2}]", (int)response.StatusCode, request.Method, request.Url.ToString())) + : base(string.Format("HTTP request failed: [{0}:{1}] [{2}] at [{3}]", (int)response.StatusCode, response.StatusCode, request.Method, request.Url.ToString())) { Request = request; Response = response; diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index b697320b3..99c966891 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -103,6 +103,7 @@ + diff --git a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs index 6f57b8132..2dc971663 100644 --- a/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs +++ b/src/NzbDrone.Core/Indexers/HttpIndexerBase.cs @@ -152,6 +152,15 @@ namespace NzbDrone.Core.Indexers _logger.Warn("{0} {1} {2}", this, url, webException.Message); } } + catch (HttpException httpException) + { + if ((int)httpException.Response.StatusCode == 429) + { + _logger.Warn("API Request Limit reached for {0}", this); + } + + _logger.Warn("{0} {1}", this, httpException.Message); + } catch (RequestLimitReachedException) { // TODO: Backoff for x period. diff --git a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs index 650c2b1d2..dd0b23fb4 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/Newznab.cs @@ -1,12 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using FluentValidation.Results; using NLog; -using NzbDrone.Common; using NzbDrone.Common.Http; using NzbDrone.Core.Configuration; -using NzbDrone.Core.Indexers.Exceptions; using NzbDrone.Core.Parser; using NzbDrone.Core.ThingiProvider; diff --git a/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs b/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs index 7eca4f95d..84a72655f 100644 --- a/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs @@ -5,6 +5,7 @@ using System.Linq; using NLog; using NzbDrone.Common; using NzbDrone.Common.Disk; +using NzbDrone.Common.Exceptron; using NzbDrone.Common.Instrumentation.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles.Events; @@ -49,29 +50,29 @@ namespace NzbDrone.Core.MediaFiles switch (_configService.FileDate) { case FileDateType.LocalAirDate: - { - var airDate = episodes.First().AirDate; - var airTime = series.AirTime; - - if (airDate.IsNullOrWhiteSpace() || airTime.IsNullOrWhiteSpace()) { - return false; - } + var airDate = episodes.First().AirDate; + var airTime = series.AirTime; - return ChangeFileDateToLocalAirDate(episodeFilePath, airDate, airTime); - } + if (airDate.IsNullOrWhiteSpace() || airTime.IsNullOrWhiteSpace()) + { + return false; + } + + return ChangeFileDateToLocalAirDate(episodeFilePath, airDate, airTime); + } case FileDateType.UtcAirDate: - { - var airDateUtc = episodes.First().AirDateUtc; - - if (!airDateUtc.HasValue) { - return false; - } + var airDateUtc = episodes.First().AirDateUtc; - return ChangeFileDateToUtcAirDate(episodeFilePath, airDateUtc.Value); - } + if (!airDateUtc.HasValue) + { + return false; + } + + return ChangeFileDateToUtcAirDate(episodeFilePath, airDateUtc.Value); + } } return false; @@ -99,7 +100,7 @@ namespace NzbDrone.Core.MediaFiles if (ChangeFileDate(episodeFile, message.Series, episodesInFile)) { updated.Add(episodeFile); - } + } } if (updated.Any()) @@ -163,6 +164,7 @@ namespace NzbDrone.Core.MediaFiles catch (Exception ex) { + ex.ExceptronIgnoreOnMono(); _logger.WarnException("Unable to set date of file [" + filePath + "]", ex); } }