UNIT6-open / TemplateEngine.Docx

Smart docx template engine for .NET
Other
408 stars 136 forks source link

Tables without rows give a cryptic error message #58

Open bruno-brant opened 5 years ago

bruno-brant commented 5 years ago

I made a mistake building the Content and created a Table with zero rows.

The error thrown was

Message: System.ArgumentNullException : Value cannot be null.
Parameter name: source

Result StackTrace:  
at System.Linq.Enumerable.SelectMany[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at TemplateEngine.Docx.Container.<>c__DisplayClass26_0.<get_FieldNames>b__3(TableContent t)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Set`1.UnionWith(IEnumerable`1 other)
   at System.Linq.Enumerable.DistinctIterator`1.FillSet()
   at System.Linq.Enumerable.DistinctIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TemplateEngine.Docx.RepeatContent.get_FieldNames()
   at TemplateEngine.Docx.Processors.RepeatProcessor.FillContent(XElement contentControl, IContentItem item)
   at TemplateEngine.Docx.Processors.RepeatProcessor.FillContent(XElement contentControl, IEnumerable`1 items)
   at TemplateEngine.Docx.Processors.ContentProcessor.FillContent(XElement content, IEnumerable`1 data)
   at TemplateEngine.Docx.Processors.ContentProcessor.FillContent(XElement content, Content data)
   at TemplateEngine.Docx.TemplateProcessor.FillContent(Content content)
   at Slingshot.Templates.WordTemplate.WordTemplateProcessor.FillContent(Content valuesToFill)
   at Slingshot.Templates.WordTemplate.WordTemplateProcessor.FillContentFromModel(Object model)
   at Slingshot.PropostaDeServico.PropostaDeServicoTemplate.GerarDocumento(IArchive archive, PropostaDeServicoModel model)
   at Slingshot.PipelineAtm.PipelineAtmFull.GerarAtmFull(Stream sementeStream, Stream outputStream)
   at Slingshot.PipelineAtm.Tests.PipelineAtmFullTests.CreateOutput()
Result Message: 
System.ArgumentNullException : Value cannot be null.
Parameter name: source

I traced it to an attempt by a RepeatContent to enumerate all its children, on class Container, at FieldNames:

var repeatsFieldNames = Repeats == null
   ? new List<string>()
   : Repeats.Select(t => t.Name).Concat(Repeats.SelectMany(t => t.Items.SelectMany(r => r.FieldNames)));

I'm not sure if TableContents must have rows, but I recommend a better error message to save us the time diagnosing this situation.