From c9a77e99a0ac58852a73a41898a117a5cedc2dad Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 4 Feb 2014 18:46:03 -0800 Subject: [PATCH] Error handling in migration to new quality --- src/NzbDrone.Common/Serializer/Json.cs | 13 ++++++++++--- .../Migration/036_update_with_quality_converters.cs | 13 ++++++++++--- .../Clients/Sabnzbd/SabCommunicationProxy.cs | 10 +++++----- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Common/Serializer/Json.cs b/src/NzbDrone.Common/Serializer/Json.cs index 9a9b0e04b..1abb2019b 100644 --- a/src/NzbDrone.Common/Serializer/Json.cs +++ b/src/NzbDrone.Common/Serializer/Json.cs @@ -41,15 +41,22 @@ namespace NzbDrone.Common.Serializer return JsonConvert.DeserializeObject(json, type, SerializerSetting); } - public static T TryDeserialize(string json) where T : new() + public static bool TryDeserialize(string json, out T result) where T : new() { try { - return Deserialize(json); + result = Deserialize(json); + return true; } catch (JsonReaderException ex) { - return default(T); + result = default(T); + return false; + } + catch (JsonSerializationException ex) + { + result = default(T); + return false; } } diff --git a/src/NzbDrone.Core/Datastore/Migration/036_update_with_quality_converters.cs b/src/NzbDrone.Core/Datastore/Migration/036_update_with_quality_converters.cs index 090ddd33a..a89c715e0 100644 --- a/src/NzbDrone.Core/Datastore/Migration/036_update_with_quality_converters.cs +++ b/src/NzbDrone.Core/Datastore/Migration/036_update_with_quality_converters.cs @@ -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(qualityJson); + QualityModel quality; + + if (!Json.TryDeserialize(qualityJson, out quality)) + { + continue; + } var qualityNewJson = qualityModelConverter.ToDB(quality); diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabCommunicationProxy.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabCommunicationProxy.cs index a372185eb..8e181a25e 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabCommunicationProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabCommunicationProxy.cs @@ -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(ProcessRequest(request, action)); - if (response == null) + SabAddResponse response; + + if (!Json.TryDeserialize(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(response.Content); + SabJsonError result; - if (result == null) + if (!Json.TryDeserialize(response.Content, out result)) { //Handle plain text responses from SAB result = new SabJsonError();