Skip to content

Commit

Permalink
Better create/find error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Aug 7, 2020
1 parent b7e2690 commit c21d3bf
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 45 deletions.
12 changes: 9 additions & 3 deletions uSync8.ContentEdition/Serializers/ContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ protected virtual XElement SerializeSchedule(IContent item, SyncSerializerOption

protected override SyncAttempt<IContent> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var attempt = FindOrCreate(node);
if (!attempt.Success) throw attempt.Exception;

var item = FindOrCreate(node);
var item = attempt.Result;

var details = new List<uSyncChange>();

Expand Down Expand Up @@ -480,10 +482,14 @@ private void UnpublishMissingCultures(IContent item, string[] allCultures)

#endregion

protected override IContent CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IContent> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var parentId = parent != null ? parent.Id : -1;
return contentService.Create(alias, parentId, itemType);
var item = contentService.Create(alias, parentId, itemType);
if (item == null)
return Attempt.Fail(item, new ArgumentException($"Unable to create content item of type {itemType}"));

return Attempt.Succeed(item);
}

#region Finders
Expand Down
24 changes: 12 additions & 12 deletions uSync8.ContentEdition/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,14 @@ protected Attempt<List<uSyncChange>, string> DeserializeProperties(TObject item,
logger.Verbose(serializerType, "Derserialize Property {0} {1}", alias, current.PropertyType.PropertyEditorAlias);

var values = property.Elements("Value").ToList();

foreach (var value in values)
{
var culture = value.Attribute("Culture").ValueOrDefault(string.Empty);
var segment = value.Attribute("Segment").ValueOrDefault(string.Empty);
var propValue = value.ValueOrDefault(string.Empty);

logger.Verbose(serializerType, "{Property} Culture {Culture} Segment {Segment}", alias, culture, segment);
logger.Verbose(serializerType, "{item} {Property} Culture {Culture} Segment {Segment}", item.Name, alias, culture, segment);

try
{
Expand All @@ -450,16 +450,16 @@ protected Attempt<List<uSyncChange>, string> DeserializeProperties(TObject item,
{
// this culture is not the default for the site, so don't use it to
// set the single language value.
logger.Warn(serializerType, "Culture {culture} in file, but is not default so not being used", culture);
logger.Warn(serializerType, "{item} Culture {culture} in file, but is not default so not being used", item.Name, culture);
continue;
}
logger.Warn(serializerType, "Cannot set value on culture {culture} because it is not avalible for this property - value in default language will be used", culture);
logger.Warn(serializerType, "{item} Cannot set value on culture {culture} because it is not avalible for this property - value in default language will be used", item.Name, culture);
culture = string.Empty;
}
else if (!item.AvailableCultures.InvariantContains(culture))
{
// this culture isn't one of the ones, that can be set on this language.
logger.Warn(serializerType, "Culture {culture} is not one of the avalible cultures, so we cannot set this value", culture);
logger.Warn(serializerType, "{item} Culture {culture} is not one of the avalible cultures, so we cannot set this value", item.Name, culture);
continue;
}
}
Expand All @@ -477,7 +477,7 @@ protected Attempt<List<uSyncChange>, string> DeserializeProperties(TObject item,
}
else
{
logger.Warn(serializerType, "Property {Alias} contains a value that has no culture but this property varies by culture so this value has no effect", alias);
logger.Warn(serializerType, "{item} Property {Alias} contains a value that has no culture but this property varies by culture so this value has no effect", item.Name, alias);
continue;
}
}
Expand All @@ -495,23 +495,23 @@ protected Attempt<List<uSyncChange>, string> DeserializeProperties(TObject item,
string.IsNullOrEmpty(culture) ? null : culture,
string.IsNullOrEmpty(segment) ? null : segment);

logger.Debug(serializerType, "Property {alias} value set", alias);
logger.Debug(serializerType, "Property {item} set {alias} value", item.Name, alias);
logger.Verbose(serializerType, "{Id} Property [{alias}] : {itemValue}", item.Id, alias, itemValue);
}
}
catch (Exception ex)
{
// capture here to be less agressive with failure.
// if one property fails the rest will still go in.
logger.Warn(serializerType, "Failed to set [{alias}] {propValue} Ex: {Exception}", alias, propValue, ex.ToString());
logger.Warn(serializerType, "{item} Failed to set [{alias}] {propValue} Ex: {Exception}", item.Name, alias, propValue, ex.ToString());
errors += $"Failed to set [{alias}] {ex.Message} <br/>";
}
}
}
else
{
logger.Warn(serializerType, "DeserializeProperties: item {Name} doesn't have property {alias} but its in the xml", item.Name, alias);
errors += $"Item {Name} doesn't contain {alias}";
errors += $"{item.Name} does not container property {alias}";
}
}

Expand Down Expand Up @@ -599,18 +599,18 @@ public override bool IsValid(XElement node)

// these are the functions using the simple 'getItem(alias)'
// that we cannot use for content/media trees.
protected override TObject FindOrCreate(XElement node)
protected override Attempt<TObject> FindOrCreate(XElement node)
{
TObject item = FindItem(node);
if (item != null) return item;
if (item != null) return Attempt.Succeed(item);

var alias = node.GetAlias();

var parentKey = node.Attribute("Parent").ValueOrDefault(Guid.Empty);
if (parentKey != Guid.Empty)
{
item = FindItem(alias, parentKey);
if (item != null) return item;
if (item != null) return Attempt.Succeed(item);
}

// create
Expand Down
23 changes: 14 additions & 9 deletions uSync8.ContentEdition/Serializers/ContentTemplateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ protected override XElement SerializeInfo(IContent item, SyncSerializerOptions o

protected override SyncAttempt<IContent> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var item = FindOrCreate(node);
if (item.Trashed)
{
// TODO: Where has changed trashed state gone?
}
var attempt = FindOrCreate(node);
if (!attempt.Success) throw attempt.Exception;

var item = attempt.Result;

var details = new List<uSyncChange>();

Expand Down Expand Up @@ -111,16 +110,22 @@ protected override IContent FindItem(Guid key)
protected override IContent FindItem(int id)
=> contentService.GetBlueprintById(id);

protected override IContent CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IContent> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var contentType = contentTypeService.Get(itemType);
if (contentType == null) return null;
if (contentType == null) return
Attempt.Fail<IContent>(null, new ArgumentException($"Missing content Type {itemType}"));

IContent item;
if (parent != null)
{
return new Content(alias, (IContent)parent, contentType);
item = new Content(alias, (IContent)parent, contentType);
}
return new Content(alias, -1, contentType);
else {
item = new Content(alias, -1, contentType);
}

return Attempt.Succeed(item);
}

protected override Attempt<string> DoSaveOrPublish(IContent item, XElement node, SyncSerializerOptions options)
Expand Down
10 changes: 7 additions & 3 deletions uSync8.ContentEdition/Serializers/MediaSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public MediaSerializer(

protected override SyncAttempt<IMedia> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var item = FindOrCreate(node);
var attempt = FindOrCreate(node);
if (!attempt.Success) throw attempt.Exception;

var item = attempt.Result;

var details = DeserializeBase(item, node, options);

return SyncAttempt<IMedia>.Succeed(item.Name, item, ChangeType.Import, details.ToList());
Expand Down Expand Up @@ -177,11 +181,11 @@ private string GetFilePath(string value)
return value;
}

protected override IMedia CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IMedia> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var parentId = parent != null ? parent.Id : -1;
var item = mediaService.CreateMedia(alias, parentId, itemType);
return item;
return Attempt.Succeed((IMedia)item);
}

protected override IMedia FindItem(int id)
Expand Down
10 changes: 7 additions & 3 deletions uSync8.Core/Serialization/Serializers/ContentTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Xml.Linq;

using Umbraco.Core;
Expand Down Expand Up @@ -95,7 +96,10 @@ private XElement SerailizeTemplates(IContentType item)

protected override SyncAttempt<IContentType> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var item = FindOrCreate(node);
var attempt = FindOrCreate(node);
if (!attempt.Success) throw attempt.Exception;

var item = attempt.Result;

var details = new List<uSyncChange>();

Expand Down Expand Up @@ -220,7 +224,7 @@ private IEnumerable<uSyncChange> DeserializeTemplates(IContentType item, XElemen
}


protected override IContentType CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IContentType> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var item = new ContentType(-1)
{
Expand All @@ -237,7 +241,7 @@ protected override IContentType CreateItem(string alias, ITreeEntity parent, str
item.SetParent(parent);
}

return item;
return Attempt.Succeed((IContentType)item);
}

protected override void SaveContainer(EntityContainer container)
Expand Down
13 changes: 8 additions & 5 deletions uSync8.Core/Serialization/Serializers/DataTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ protected override SyncAttempt<IDataType> DeserializeCore(XElement node, SyncSer
var name = info.Element("Name").ValueOrDefault(string.Empty);
var key = node.GetKey();

var item = FindOrCreate(node);
if (item == null) throw new ArgumentException($"Cannot find underling datatype for {name}");
var attempt = FindOrCreate(node);
if (!attempt.Success)
throw attempt.Exception;

var details = new List<uSyncChange>();
var item = attempt.Result;

// basic
if (item.Name != name)
Expand Down Expand Up @@ -209,10 +211,11 @@ private XElement SerializeConfiguration(IDataType item)
}


protected override IDataType CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IDataType> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var editorType = FindDataEditor(itemType);
if (editorType == null) return null;
if (editorType == null)
return Attempt.Fail<IDataType>(null, new ArgumentException($"(Missing Package?) DataEditor {itemType} is not installed"));

var item = new DataType(editorType, -1)
{
Expand All @@ -222,7 +225,7 @@ protected override IDataType CreateItem(string alias, ITreeEntity parent, string
if (parent != null)
item.SetParent(parent);

return item;
return Attempt.Succeed((IDataType)item);
}

private IDataEditor FindDataEditor(string alias)
Expand Down
10 changes: 7 additions & 3 deletions uSync8.Core/Serialization/Serializers/MediaTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Xml.Linq;

using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
Expand Down Expand Up @@ -62,7 +63,10 @@ protected override SyncAttempt<IMediaType> DeserializeCore(XElement node, SyncSe

var details = new List<uSyncChange>();

var item = FindOrCreate(node);
var attempt = FindOrCreate(node);
if (!attempt.Success) throw attempt.Exception;

var item = attempt.Result;

details.AddRange(DeserializeBase(item, node));
details.AddRange(DeserializeTabs(item, node));
Expand All @@ -89,7 +93,7 @@ public override SyncAttempt<IMediaType> DeserializeSecondPass(IMediaType item, X
return SyncAttempt<IMediaType>.Succeed(item.Name, item, ChangeType.Import, details);
}

protected override IMediaType CreateItem(string alias, ITreeEntity parent, string itemType)
protected override Attempt<IMediaType> CreateItem(string alias, ITreeEntity parent, string itemType)
{
var item = new MediaType(-1)
{
Expand All @@ -104,7 +108,7 @@ protected override IMediaType CreateItem(string alias, ITreeEntity parent, strin
item.SetParent(parent);
}

return item;
return Attempt.Succeed((IMediaType)item);
}
}
}
11 changes: 8 additions & 3 deletions uSync8.Core/Serialization/Serializers/MemberTypeSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Management;
using System.Xml.Linq;

using Umbraco.Core;
Expand Down Expand Up @@ -100,7 +101,11 @@ protected override XElement SerializeProperties(IMemberType item)

protected override SyncAttempt<IMemberType> DeserializeCore(XElement node, SyncSerializerOptions options)
{
var item = FindOrCreate(node);
var attempt = FindOrCreate(node);
if (!attempt.Success)
throw attempt.Exception;

var item = attempt.Result;

var details = new List<uSyncChange>();

Expand Down Expand Up @@ -143,7 +148,7 @@ protected override IEnumerable<uSyncChange> DeserializeExtraProperties(IMemberTy
return changes;
}

protected override IMemberType CreateItem(string alias, ITreeEntity parent, string extra)
protected override Attempt<IMemberType> CreateItem(string alias, ITreeEntity parent, string extra)
{
var item = new MemberType(-1)
{
Expand All @@ -159,7 +164,7 @@ protected override IMemberType CreateItem(string alias, ITreeEntity parent, stri
}


return item;
return Attempt.Succeed((IMemberType)item);
}
}
}
4 changes: 2 additions & 2 deletions uSync8.Core/Serialization/SyncContainerSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public SyncContainerSerializerBase(IEntityService entityService, ILogger logger,
this.containerType = containerType;
}

protected override TObject FindOrCreate(XElement node)
protected override Attempt<TObject> FindOrCreate(XElement node)
{

TObject item = FindItem(node);
if (item != null) return item;
if (item != null) return Attempt.Succeed(item);

logger.Debug(serializerType, "FindOrCreate: Creating");

Expand Down
5 changes: 3 additions & 2 deletions uSync8.Core/Serialization/SyncTreeSerializerBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Xml.Linq;

using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Services;
Expand All @@ -15,7 +16,7 @@ protected SyncTreeSerializerBase(IEntityService entityService, ILogger logger)
{
}

protected abstract TObject CreateItem(string alias, ITreeEntity parent, string itemType);
protected abstract Attempt<TObject> CreateItem(string alias, ITreeEntity parent, string itemType);


#region Getters
Expand All @@ -28,7 +29,7 @@ protected virtual string GetItemBaseType(XElement node)
#region Finders
// Finders - used on importing, getting things that are already there (or maybe not)

protected abstract TObject FindOrCreate(XElement node);
protected abstract Attempt<TObject> FindOrCreate(XElement node);

protected TObject FindItem(Guid key, string alias)
{
Expand Down

0 comments on commit c21d3bf

Please sign in to comment.