diff --git a/src/Nitride.Yaml/ParseYamlHeader.cs b/src/Nitride.Yaml/ParseYamlHeader.cs index 472ddd1..95f7666 100644 --- a/src/Nitride.Yaml/ParseYamlHeader.cs +++ b/src/Nitride.Yaml/ParseYamlHeader.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -30,6 +31,8 @@ public class ParseYamlHeader : OperationBase this.RemoveHeader = true; } + public Func? EntityError { get; set; } + /// /// Gets or sets a value indicating whether the header should be removed /// and the text content be removed. @@ -52,10 +55,18 @@ public class ParseYamlHeader : OperationBase // Process through the files. We only care about the text ones // and we'll put a default TModel in for those that don't have a // header. - return input.SelectEntity( - ( - entity, - content) => this.Parse(entity, content, deserializer)); + return input + .SelectEntity( + ( + entity, + content) => this.Parse(entity, content, deserializer)); + } + + public ParseYamlHeader WithEntityError(Func? value) + { + this.EntityError = value; + + return this; } /// @@ -138,8 +149,24 @@ public class ParseYamlHeader : OperationBase // Pull out the model so we can append it later. string yaml = buffer.ToString(); + TModel? model; - TModel? model = deserializer.Deserialize(yaml); + try + { + model = deserializer.Deserialize(yaml); + } + catch (Exception exception) + { + ParseYamlHeaderErrorHandling disposition = this.EntityError?.Invoke(entity, yaml, exception) + ?? ParseYamlHeaderErrorHandling.Throw; + + if (disposition == ParseYamlHeaderErrorHandling.Ignore) + { + return entity; + } + + throw; + } // If we are not removing the header, then we're done. if (!this.RemoveHeader) diff --git a/src/Nitride.Yaml/ParseYamlHeaderErrorHandling.cs b/src/Nitride.Yaml/ParseYamlHeaderErrorHandling.cs new file mode 100644 index 0000000..b6bee18 --- /dev/null +++ b/src/Nitride.Yaml/ParseYamlHeaderErrorHandling.cs @@ -0,0 +1,14 @@ +namespace Nitride.Yaml; + +public enum ParseYamlHeaderErrorHandling +{ + /// + /// Throw an exception if an error happens. + /// + Throw, + + /// + /// If an error happens, then include without additional processing. + /// + Ignore, +}