Better handling of SAB not returning json to addfile

Fixed: Handling of unexpected responses from SABnzbd when adding releases
pull/3113/head
Mark McDowall 11 years ago
parent a01f2dd862
commit 4a7bd5b849

@ -41,6 +41,18 @@ namespace NzbDrone.Common.Serializer
return JsonConvert.DeserializeObject(json, type, SerializerSetting); return JsonConvert.DeserializeObject(json, type, SerializerSetting);
} }
public static T TryDeserialize<T>(string json) where T : new()
{
try
{
return Deserialize<T>(json);
}
catch (JsonReaderException ex)
{
return default(T);
}
}
public static string ToJson(this object obj) public static string ToJson(this object obj)
{ {
return JsonConvert.SerializeObject(obj, SerializerSetting); return JsonConvert.SerializeObject(obj, SerializerSetting);

@ -6,6 +6,11 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
{ {
public class SabAddResponse public class SabAddResponse
{ {
public SabAddResponse()
{
Ids = new List<String>();
}
public bool Status { get; set; } public bool Status { get; set; }
[JsonProperty(PropertyName = "nzo_ids")] [JsonProperty(PropertyName = "nzo_ids")]

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using Newtonsoft.Json;
using NLog; using NLog;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -9,7 +10,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
{ {
public interface ISabCommunicationProxy public interface ISabCommunicationProxy
{ {
string DownloadNzb(Stream nzb, string name, string category, int priority); SabAddResponse DownloadNzb(Stream nzb, string name, string category, int priority);
void RemoveFrom(string source, string id); void RemoveFrom(string source, string id);
string ProcessRequest(IRestRequest restRequest, string action); string ProcessRequest(IRestRequest restRequest, string action);
} }
@ -25,14 +26,22 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
_logger = logger; _logger = logger;
} }
public string DownloadNzb(Stream nzb, string title, string category, int priority) public SabAddResponse DownloadNzb(Stream nzb, string title, string category, int priority)
{ {
var request = new RestRequest(Method.POST); var request = new RestRequest(Method.POST);
var action = String.Format("mode=addfile&cat={0}&priority={1}", category, priority); var action = String.Format("mode=addfile&cat={0}&priority={1}", category, priority);
request.AddFile("name", ReadFully(nzb), title, "application/x-nzb"); request.AddFile("name", ReadFully(nzb), title, "application/x-nzb");
return ProcessRequest(request, action); var response = Json.TryDeserialize<SabAddResponse>(ProcessRequest(request, action));
if (response == null)
{
response = new SabAddResponse();
response.Status = true;
}
return response;
} }
public void RemoveFrom(string source, string id) public void RemoveFrom(string source, string id)
@ -67,6 +76,8 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
_configService.SabUsername, _configService.SabUsername,
_configService.SabPassword); _configService.SabPassword);
_logger.Trace(url);
return new RestClient(url); return new RestClient(url);
} }
@ -77,9 +88,28 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
throw new ApplicationException("Unable to connect to SABnzbd, please check your settings"); throw new ApplicationException("Unable to connect to SABnzbd, please check your settings");
} }
var result = Json.Deserialize<SabJsonError>(response.Content); var result = Json.TryDeserialize<SabJsonError>(response.Content);
if (result == null)
{
//Handle plain text responses from SAB
result = new SabJsonError();
if (response.Content.StartsWith("error", StringComparison.InvariantCultureIgnoreCase))
{
result.Status = false;
result.Error = response.Content.Replace("error: ", "");
}
else
{
result.Status = true;
}
result.Error = response.Content.Replace("error: ", "");
}
if (result.Status != null && result.Status.Equals("false", StringComparison.InvariantCultureIgnoreCase)) if (!result.Status)
throw new ApplicationException(result.Error); throw new ApplicationException(result.Error);
} }

@ -2,7 +2,7 @@
{ {
public class SabJsonError public class SabJsonError
{ {
public string Status { get; set; } public bool Status { get; set; }
public string Error { get; set; } public string Error { get; set; }
} }
} }

@ -55,11 +55,14 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
using (var nzb = _httpProvider.DownloadStream(url)) using (var nzb = _httpProvider.DownloadStream(url))
{ {
_logger.Info("Adding report [{0}] to the queue.", title); _logger.Info("Adding report [{0}] to the queue.", title);
var response = Json.Deserialize<SabAddResponse>(_sabCommunicationProxy.DownloadNzb(nzb, title, category, priority)); var response = _sabCommunicationProxy.DownloadNzb(nzb, title, category, priority);
_logger.Debug("Queue Response: [{0}]", response.Status); if (response != null && response.Ids.Any())
{
return response.Ids.First();
}
return response.Ids.First(); return null;
} }
} }
@ -240,7 +243,7 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
{ {
var result = Json.Deserialize<SabJsonError>(response); var result = Json.Deserialize<SabJsonError>(response);
if (result.Status != null && result.Status.Equals("false", StringComparison.InvariantCultureIgnoreCase)) if (result.Status)
throw new ApplicationException(result.Error); throw new ApplicationException(result.Error);
} }
} }

Loading…
Cancel
Save