From 204bd49d4643110c8d242abc2fc6c0fca8369289 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Wed, 22 Apr 2020 08:58:13 +0100 Subject: [PATCH] #93 - Better error handling when an XML file is corrupt, and clean up fails. (#94) Co-authored-by: Kevin Jump --- uSync8.BackOffice/Services/SyncFileService.cs | 16 ++++++++-- .../SyncHandlers/SyncHandlerBase.cs | 30 ++++++++++++------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/uSync8.BackOffice/Services/SyncFileService.cs b/uSync8.BackOffice/Services/SyncFileService.cs index f245ac3d..f30b2f1c 100644 --- a/uSync8.BackOffice/Services/SyncFileService.cs +++ b/uSync8.BackOffice/Services/SyncFileService.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; @@ -132,9 +134,17 @@ public XElement LoadXElement(string file) { EnsureFileExists(file); - using (var stream = OpenRead(file)) + try { - return XElement.Load(stream); + using (var stream = OpenRead(file)) + { + return XElement.Load(stream); + } + } + catch(Exception ex) + { + logger.Warn("Error while reading in {file} {message}", file, ex.Message); + throw new Exception($"Error while reading in {file}", ex); } } diff --git a/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs b/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs index 19756eb2..d0cb9a2c 100644 --- a/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs +++ b/uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs @@ -895,25 +895,33 @@ protected virtual void CleanUp(TObject item, string newFile, string folder) { if (!file.InvariantEquals(physicalFile)) { - var node = syncFileService.LoadXElement(file); - - // if this xml file matches the item we have just saved. - - if (!node.IsEmptyItem() || node.GetEmptyAction() != SyncActionType.Rename) + try { - // the node isn't empty, or its not a rename (because all clashes become renames) + var node = syncFileService.LoadXElement(file); + + // if this xml file matches the item we have just saved. - if (DoItemsMatch(node, item)) + if (!node.IsEmptyItem() || node.GetEmptyAction() != SyncActionType.Rename) { - logger.Debug(handlerType, "Duplicate {file} of {alias}, saving as rename", Path.GetFileName(file), this.GetItemAlias(item)); + // the node isn't empty, or its not a rename (because all clashes become renames) - var attempt = serializer.SerializeEmpty(item, SyncActionType.Rename, node.GetAlias()); - if (attempt.Success) + if (DoItemsMatch(node, item)) { - syncFileService.SaveXElement(attempt.Item, file); + logger.Debug(handlerType, "Duplicate {file} of {alias}, saving as rename", Path.GetFileName(file), this.GetItemAlias(item)); + + var attempt = serializer.SerializeEmpty(item, SyncActionType.Rename, node.GetAlias()); + if (attempt.Success) + { + syncFileService.SaveXElement(attempt.Item, file); + } } } } + catch(Exception ex) + { + logger.Warn(handlerType, "Error during cleanup of existing files {message}", ex.Message); + // cleanup should fail silently ? - because it can impact on normal Umbraco operations? + } } }