From eb98a7e8bea888db62223aed79528e07a911cad4 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 16 Apr 2020 22:16:50 +0200 Subject: [PATCH] Improved error message when nzb download contains an newznab error instead --- src/NzbDrone.Common/Extensions/XmlExtensions.cs | 16 ++++++++++++++++ .../Download/NzbValidationServiceFixture.cs | 12 ++++++++++++ .../Files/Nzbs/NewznabError.nzb | 2 ++ .../Download/NzbValidationService.cs | 9 +++++++++ src/NzbDrone.Core/Download/UsenetClientBase.cs | 2 +- 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core.Test/Files/Nzbs/NewznabError.nzb diff --git a/src/NzbDrone.Common/Extensions/XmlExtensions.cs b/src/NzbDrone.Common/Extensions/XmlExtensions.cs index 84b163165..209dd826a 100644 --- a/src/NzbDrone.Common/Extensions/XmlExtensions.cs +++ b/src/NzbDrone.Common/Extensions/XmlExtensions.cs @@ -11,5 +11,21 @@ public static IEnumerable FindDecendants(this XContainer container, st { return container.Descendants().Where(c => c.Name.LocalName.Equals(localName, StringComparison.InvariantCultureIgnoreCase)); } + + public static bool TryGetAttributeValue(this XElement element, string name, out string value) + { + var attr = element.Attribute(name); + + if (attr != null) + { + value = attr.Value; + return true; + } + else + { + value = null; + return false; + } + } } } diff --git a/src/NzbDrone.Core.Test/Download/NzbValidationServiceFixture.cs b/src/NzbDrone.Core.Test/Download/NzbValidationServiceFixture.cs index 557a28ae0..fa694dcf9 100644 --- a/src/NzbDrone.Core.Test/Download/NzbValidationServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Download/NzbValidationServiceFixture.cs @@ -1,4 +1,5 @@ using System.IO; +using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Download; using NzbDrone.Core.Test.Framework; @@ -31,6 +32,17 @@ public void should_throw_when_no_files() Assert.Throws(() => Subject.Validate(filename, fileContent)); } + [Test] + public void should_throw_on_newznab_error() + { + var filename = "NewznabError"; + var fileContent = GivenNzbFile(filename); + + var ex = Assert.Throws(() => Subject.Validate(filename, fileContent)); + + ex.Message.Should().Contain("201 - Incorrect parameter"); + } + [Test] public void should_validate_nzb() { diff --git a/src/NzbDrone.Core.Test/Files/Nzbs/NewznabError.nzb b/src/NzbDrone.Core.Test/Files/Nzbs/NewznabError.nzb new file mode 100644 index 000000000..9ebe1890d --- /dev/null +++ b/src/NzbDrone.Core.Test/Files/Nzbs/NewznabError.nzb @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/NzbDrone.Core/Download/NzbValidationService.cs b/src/NzbDrone.Core/Download/NzbValidationService.cs index 5385a06a8..571729ccd 100644 --- a/src/NzbDrone.Core/Download/NzbValidationService.cs +++ b/src/NzbDrone.Core/Download/NzbValidationService.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Linq; using System.Xml; @@ -27,6 +28,14 @@ public void Validate(string filename, byte[] fileContent) throw new InvalidNzbException("Invalid NZB: No Root element [{0}]", filename); } + // nZEDb has an bug in their error reporting code spitting out invalid http status codes + if (nzb.Name.LocalName.Equals("error") && + nzb.TryGetAttributeValue("code", out var code) && + nzb.TryGetAttributeValue("description", out var description)) + { + throw new InvalidNzbException("Invalid NZB: Contains indexer error: {0} - {1}", code, description); + } + if (!nzb.Name.LocalName.Equals("nzb")) { throw new InvalidNzbException("Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}' [{1}]", nzb.Name.LocalName, filename); diff --git a/src/NzbDrone.Core/Download/UsenetClientBase.cs b/src/NzbDrone.Core/Download/UsenetClientBase.cs index 790985629..27afbd704 100644 --- a/src/NzbDrone.Core/Download/UsenetClientBase.cs +++ b/src/NzbDrone.Core/Download/UsenetClientBase.cs @@ -53,7 +53,7 @@ public override string Download(RemoteMovie remoteMovie) if (ex.Response.StatusCode == HttpStatusCode.NotFound) { _logger.Error(ex, "Downloading nzb file for movie '{0}' failed since it no longer exists ({1})", remoteMovie.Release.Title, url); - throw new ReleaseUnavailableException(remoteMovie.Release, "Downloading torrent failed", ex); + throw new ReleaseUnavailableException(remoteMovie.Release, "Downloading nzb failed", ex); } if ((int)ex.Response.StatusCode == 429)