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); 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 try
{ {
return Deserialize<T>(json); result = Deserialize<T>(json);
return true;
} }
catch (JsonReaderException ex) 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() 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(ConvertQualityProfiles);
Execute.WithConnection(ConvertQualityModels); Execute.WithConnection(ConvertQualityModels);
} }
@ -80,7 +82,12 @@ namespace NzbDrone.Core.Datastore.Migration
var id = qualityModelReader.GetInt32(0); var id = qualityModelReader.GetInt32(0);
var qualityJson = qualityModelReader.GetString(1); 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); var qualityNewJson = qualityModelConverter.ToDB(quality);

@ -32,9 +32,9 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd
request.AddFile("name", ReadFully(nzb), title, "application/x-nzb"); request.AddFile("name", ReadFully(nzb), title, "application/x-nzb");
var response = Json.TryDeserialize<SabAddResponse>(ProcessRequest(request, action)); SabAddResponse response;
if (response == null) if (!Json.TryDeserialize<SabAddResponse>(ProcessRequest(request, action), out response))
{ {
response = new SabAddResponse(); response = new SabAddResponse();
response.Status = true; 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"); 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 //Handle plain text responses from SAB
result = new SabJsonError(); result = new SabJsonError();

Loading…
Cancel
Save