Skip to content

Commit

Permalink
#93 - Better error handling when an XML file is corrupt, and clean up…
Browse files Browse the repository at this point in the history
… fails. (#94)

Co-authored-by: Kevin Jump <[email protected]>
  • Loading branch information
KevinJump and Kevin Jump authored Apr 22, 2020
1 parent 7a4f1bb commit 204bd49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
16 changes: 13 additions & 3 deletions uSync8.BackOffice/Services/SyncFileService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<SyncFileService>("Error while reading in {file} {message}", file, ex.Message);
throw new Exception($"Error while reading in {file}", ex);
}
}

Expand Down
30 changes: 19 additions & 11 deletions uSync8.BackOffice/SyncHandlers/SyncHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
}
}

Expand Down

0 comments on commit 204bd49

Please sign in to comment.