Improve messaging when NZB contains invalid XML

(cherry picked from commit 728df146ada115a367bf1ce808482a4625e6098d)
This commit is contained in:
Mark McDowall 2025-04-28 15:58:25 -07:00 committed by Bogdan
parent 9f229bb684
commit 47c32c9963

View file

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
@ -14,10 +15,13 @@ namespace NzbDrone.Core.Download
public class NzbValidationService : IValidateNzbs public class NzbValidationService : IValidateNzbs
{ {
public void Validate(string filename, byte[] fileContent) public void Validate(string filename, byte[] fileContent)
{
try
{ {
var reader = new StreamReader(new MemoryStream(fileContent)); var reader = new StreamReader(new MemoryStream(fileContent));
using (var xmlTextReader = XmlReader.Create(reader, new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true })) using (var xmlTextReader = XmlReader.Create(reader,
new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, IgnoreComments = true }))
{ {
var xDoc = XDocument.Load(xmlTextReader); var xDoc = XDocument.Load(xmlTextReader);
var nzb = xDoc.Root; var nzb = xDoc.Root;
@ -37,7 +41,8 @@ namespace NzbDrone.Core.Download
if (!nzb.Name.LocalName.Equals("nzb")) if (!nzb.Name.LocalName.Equals("nzb"))
{ {
throw new InvalidNzbException("Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}' [{1}]", nzb.Name.LocalName, filename); throw new InvalidNzbException(
"Invalid NZB: Unexpected root element. Expected 'nzb' found '{0}' [{1}]", nzb.Name.LocalName, filename);
} }
var ns = nzb.Name.Namespace; var ns = nzb.Name.Namespace;
@ -49,5 +54,15 @@ namespace NzbDrone.Core.Download
} }
} }
} }
catch (InvalidNzbException)
{
// Throw the original exception
throw;
}
catch (Exception ex)
{
throw new InvalidNzbException("Invalid NZB: Unable to parse [{0}]", ex, filename);
}
}
} }
} }