Error handling in migration to new quality

pull/4/head
Mark McDowall 11 years ago
parent fe4f3d5d1e
commit c9a77e99a0

@ -41,15 +41,22 @@ namespace NzbDrone.Common.Serializer
return JsonConvert.DeserializeObject(json, type, SerializerSetting);
}
public static T TryDeserialize<T>(string json) where T : new()
public static bool TryDeserialize<T>(string json, out T result) where T : new()
{
try
{
return Deserialize<T>(json);
result = Deserialize<T>(json);
return true;
}
catch (JsonReaderException ex)
{
return default(T);
result = default(T);
return false;
}
catch (JsonSerializationException ex)
{
result = default(T);
return false;
}
}

@ -14,10 +14,12 @@ namespace NzbDrone.Core.Datastore.Migration
{
protected override void MainDbUpgrade()
{
Alter.Table("QualityProfiles").AddColumn("Items").AsString().Nullable();
if (!Schema.Table("QualityProfiles").Column("Items").Exists())
{
Alter.Table("QualityProfiles").AddColumn("Items").AsString().Nullable();
}
Execute.WithConnection(ConvertQualityProfiles);
Execute.WithConnection(ConvertQualityModels);
}
@ -80,7 +82,12 @@ namespace NzbDrone.Core.Datastore.Migration
var id = qualityModelReader.GetInt32(0);
var qualityJson = qualityModelReader.GetString(1);
var quality = Json.Deserialize<QualityModel>(qualityJson);
QualityModel quality;
if (!Json.TryDeserialize<QualityModel>(qualityJson, out quality))
{
continue;
}
var qualityNewJson = qualityModelConverter.ToDB(quality);

@ -31,10 +31,10 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
var action = String.Format("mode=addfile&cat={0}&priority={1}", category, priority);
request.AddFile("name", ReadFully(nzb), title, "application/x-nzb");
var response = Json.TryDeserialize<SabAddResponse>(ProcessRequest(request, action));
if (response == null)
SabAddResponse response;
if (!Json.TryDeserialize<SabAddResponse>(ProcessRequest(request, action), out response))
{
response = new SabAddResponse();
response.Status = true;
@ -87,9 +87,9 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
throw new ApplicationException("Unable to connect to SABnzbd, please check your settings");
}
var result = Json.TryDeserialize<SabJsonError>(response.Content);
SabJsonError result;
if (result == null)
if (!Json.TryDeserialize<SabJsonError>(response.Content, out result))
{
//Handle plain text responses from SAB
result = new SabJsonError();

Loading…
Cancel
Save