Error handling in migration to new quality

This commit is contained in:
Mark McDowall 2014-02-04 18:46:03 -08:00
parent fe4f3d5d1e
commit c9a77e99a0
3 changed files with 25 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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();