Torznab: Pull in improved error handling

7f163c3945
This commit is contained in:
flightlevel 2018-06-10 21:56:45 +10:00
parent 4ca6676be0
commit 0680d39d90
1 changed files with 109 additions and 61 deletions

View File

@ -8,11 +8,13 @@ using Jackett.Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
@ -42,8 +44,7 @@ namespace Jackett.Server.Controllers
#endif
if (queryApiKey != validApiKey)
{
context.Result = new UnauthorizedResult();
return;
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.Unauthorized, 100, "Invalid API Key");
}
}
@ -68,7 +69,7 @@ namespace Jackett.Server.Controllers
if (!parameters.ContainsKey("indexerId"))
{
indexerController.CurrentIndexer = null;
context.Result = new UnauthorizedResult();
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.NotFound, 200, "Indexer is not specified");
return;
}
@ -76,7 +77,7 @@ namespace Jackett.Server.Controllers
if (indexerId.IsNullOrEmptyOrWhitespace())
{
indexerController.CurrentIndexer = null;
context.Result = new UnauthorizedResult();
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.NotFound, 201, "Indexer is not specified (empty value)");
return;
}
@ -86,14 +87,14 @@ namespace Jackett.Server.Controllers
if (indexer == null)
{
indexerController.CurrentIndexer = null;
context.Result = new UnauthorizedResult();
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.NotFound, 201, "Indexer is not supported");
return;
}
if (!indexer.IsConfigured)
{
indexerController.CurrentIndexer = null;
context.Result = new UnauthorizedResult();
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.NotFound, 201, "Indexer is not configured");
return;
}
@ -127,7 +128,7 @@ namespace Jackett.Server.Controllers
var converter = queryType.GetMethod("ToTorznabQuery", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
if (converter == null)
{
context.Result = new BadRequestResult();
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.BadRequest, 900, "ToTorznabQuery() not found");
}
var converted = converter.Invoke(null, new object[] { query });
@ -141,7 +142,9 @@ namespace Jackett.Server.Controllers
if (!resultController.CurrentIndexer.CanHandleQuery(resultController.CurrentQuery))
{
context.Result = new BadRequestObjectResult($"{resultController.CurrentIndexer.ID} does not support the requested query. Please check the capabilities (t=caps) and make sure the search mode and categories are supported.");
context.Result = ResultsController.GetErrorActionResult(context.RouteData, HttpStatusCode.BadRequest, 201, $"{resultController.CurrentIndexer.ID} " +
$"does not support the requested query. Please check the capabilities (t=caps) and make sure the search mode and categories are supported.");
}
}
@ -265,7 +268,7 @@ namespace Jackett.Server.Controllers
return resultIndexer;
}).ToList();
manualResult.Results = tasks.Where(t => t.Status == TaskStatus.RanToCompletion).Where(t => t.Result.Releases.Count() > 0).SelectMany(t =>
manualResult.Results = tasks.Where(t => t.Status == TaskStatus.RanToCompletion).Where(t => t.Result.Releases.Any()).SelectMany(t =>
{
var searchResults = t.Result.Releases;
var indexer = t.Result.Indexer;
@ -354,6 +357,8 @@ namespace Jackett.Server.Controllers
}
}
try
{
var result = await CurrentIndexer.ResultsForQuery(CurrentQuery);
// Some trackers do not support multiple category filtering so filter the releases that match manually.
@ -409,9 +414,20 @@ namespace Jackett.Server.Controllers
return Content(xml, "application/rss+xml", Encoding.UTF8);
}
catch (Exception ex)
{
logger.Error(ex);
return GetErrorXML(900, ex.ToString());
}
}
[Route("[action]/{ignored?}")]
public IActionResult GetErrorXML(int code, string description)
{
return Content(CreateErrorXML(code, description), "application/xml", Encoding.UTF8);
}
public static string CreateErrorXML(int code, string description)
{
var xdoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
@ -420,9 +436,41 @@ namespace Jackett.Server.Controllers
new XAttribute("description", description)
)
);
return xdoc.Declaration + Environment.NewLine + xdoc;
}
var xml = xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
return Content(xml, "application/xml", Encoding.UTF8);
public static IActionResult GetErrorActionResult(RouteData routeData, HttpStatusCode status, int torznabCode, string description)
{
bool isTorznab = routeData.Values["action"].ToString().Equals("torznab", StringComparison.OrdinalIgnoreCase);
if (isTorznab)
{
ContentResult contentResult = new ContentResult
{
Content = CreateErrorXML(torznabCode, description),
ContentType = "application/xml",
StatusCode = 200
};
return contentResult;
}
else
{
switch (status)
{
case HttpStatusCode.Unauthorized:
return new UnauthorizedResult();
case HttpStatusCode.NotFound:
return new NotFoundObjectResult(description);
case HttpStatusCode.BadRequest:
return new BadRequestObjectResult(description);
default:
return new ContentResult
{
Content = description,
StatusCode = (int)status
};
}
}
}
[Route("[action]/{ignored?}")]