1
0
Fork 0
mirror of https://github.com/Jackett/Jackett synced 2025-02-23 14:51:01 +00:00

improve torznab error handling

This commit is contained in:
kaso17 2018-05-13 17:15:59 +02:00
parent cb53867b37
commit 7f163c3945

View file

@ -34,7 +34,7 @@ namespace Jackett.Controllers
return;
#endif
if (queryApiKey != validApiKey)
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.Unauthorized, 100, $"Invalid API Key");
}
}
@ -53,7 +53,7 @@ namespace Jackett.Controllers
if (!parameters.ContainsKey("indexerId"))
{
indexerController.CurrentIndexer = null;
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid parameter");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.NotFound, 200, $"indexer is not specified");
return;
}
@ -61,7 +61,7 @@ namespace Jackett.Controllers
if (indexerId.IsNullOrEmptyOrWhitespace())
{
indexerController.CurrentIndexer = null;
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid parameter");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.NotFound, 201, $"Indexer is not specified (empty value)");
return;
}
@ -71,14 +71,14 @@ namespace Jackett.Controllers
if (indexer == null)
{
indexerController.CurrentIndexer = null;
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid parameter");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.NotFound, 201, $"Indexer is not supported");
return;
}
if (!indexer.IsConfigured)
{
indexerController.CurrentIndexer = null;
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Indexer is not configured");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.NotFound, 201, $"Indexer is not configured");
return;
}
@ -104,7 +104,7 @@ namespace Jackett.Controllers
var queryType = query.GetType();
var converter = queryType.GetMethod("ToTorznabQuery", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
if (converter == null)
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.BadRequest, 900, $"ToTorznabQuery() not found");
var converted = converter.Invoke(null, new object[] { query });
var torznabQuery = converted as TorznabQuery;
resultController.CurrentQuery = torznabQuery;
@ -113,7 +113,7 @@ namespace Jackett.Controllers
return;
if (!resultController.CurrentIndexer.CanHandleQuery(resultController.CurrentQuery))
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"{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.");
actionContext.Response = ResultsController.GetErrorHttpResponseMessage(actionContext, HttpStatusCode.NotImplemented, 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.");
}
}
@ -309,6 +309,8 @@ namespace Jackett.Controllers
}
}
try
{
var result = await CurrentIndexer.ResultsForQuery(CurrentQuery);
// Some trackers do not support multiple category filtering so filter the releases that match manually.
@ -366,8 +368,29 @@ namespace Jackett.Controllers
Content = new StringContent(xml, Encoding.UTF8, "application/rss+xml")
});
}
catch (Exception e)
{
Engine.Logger.Error(e);
return GetErrorXML(900, e.Message);
}
}
public IHttpActionResult GetErrorXML(int code, string description)
static public HttpResponseMessage GetErrorHttpResponseMessage(HttpActionContext actionContext, HttpStatusCode status, int torznabCode, string description)
{
var parameters = actionContext.RequestContext.RouteData.Values;
var action = parameters["action"] as string;
if (action == "torznab")
{
return GetErrorXMLHttpResponseMessage(torznabCode, description);
}
else
{
return actionContext.Request.CreateErrorResponse(status, description);
}
}
static public HttpResponseMessage GetErrorXMLHttpResponseMessage(int code, string description)
{
var xdoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
@ -379,10 +402,15 @@ namespace Jackett.Controllers
var xml = xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
return ResponseMessage(new HttpResponseMessage()
return new HttpResponseMessage()
{
Content = new StringContent(xml, Encoding.UTF8, "application/xml")
});
};
}
public IHttpActionResult GetErrorXML(int code, string description)
{
return ResponseMessage(GetErrorXMLHttpResponseMessage(code, description));
}
[HttpGet]