Added logging for invalid recipe files

This commit is contained in:
Rob King
2015-06-02 11:03:13 +01:00
parent 1e83068b8c
commit 99f7285e80
4 changed files with 33 additions and 10 deletions

View File

@@ -136,8 +136,8 @@ namespace Orchard.Tests.Modules.Recipes.Services {
} }
[Test] [Test]
public void ParseRecipeThrowsOnInvalidXml() { public void ParseRecipeReturnsNullOnInvalidXml() {
Assert.Throws(typeof(XmlException), () => _recipeParser.ParseRecipe("<reipe></recipe>")); Assert.That(_recipeParser.ParseRecipe("<reipe></recipe>") == null);
} }
[Test] [Test]

View File

@@ -133,6 +133,7 @@ namespace Orchard.Modules.Controllers {
Module = x, Module = x,
Recipes = _recipeHarvester.HarvestRecipes(x.Descriptor.Id).Where(recipe => !recipe.IsSetupRecipe).ToList() Recipes = _recipeHarvester.HarvestRecipes(x.Descriptor.Id).Where(recipe => !recipe.IsSetupRecipe).ToList()
}) })
.ToList()
.Where(x => x.Recipes.Any()); .Where(x => x.Recipes.Any());
} }

View File

@@ -35,10 +35,20 @@ namespace Orchard.Recipes.Services {
if (extension != null) { if (extension != null) {
var recipeLocation = Path.Combine(extension.Location, extensionId, "Recipes"); var recipeLocation = Path.Combine(extension.Location, extensionId, "Recipes");
var recipeFiles = _webSiteFolder.ListFiles(recipeLocation, true); var recipeFiles = _webSiteFolder.ListFiles(recipeLocation, true);
recipes.AddRange(
from recipeFile in recipeFiles recipeFiles.Where(r => r.EndsWith(".recipe.xml", StringComparison.OrdinalIgnoreCase)).ToList().ForEach(r =>
where recipeFile.EndsWith(".recipe.xml", StringComparison.OrdinalIgnoreCase) {
select _recipeParser.ParseRecipe(_webSiteFolder.ReadFile(recipeFile))); var recipe = _recipeParser.ParseRecipe(_webSiteFolder.ReadFile(r));
if (recipe == null)
{
Logger.Error(new Exception(string.Format("Invalid recipe file: {0}", r)), "Invalid recipe file: {0}", r);
}
else
{
recipes.Add(recipe);
}
});
} }
else { else {
Logger.Error("Could not discover recipes because module '{0}' was not found.", extensionId); Logger.Error("Could not discover recipes because module '{0}' was not found.", extensionId);

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Xml; using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
using Orchard.Localization; using Orchard.Localization;
@@ -21,10 +20,23 @@ namespace Orchard.Recipes.Services {
var recipe = new Recipe(); var recipe = new Recipe();
try { try {
if (string.IsNullOrEmpty(recipeText))
{
return null;
}
XElement recipeTree;
try
{
recipeTree = XElement.Parse(recipeText, LoadOptions.PreserveWhitespace);
}
catch
{
return null;
}
var recipeSteps = new List<RecipeStep>(); var recipeSteps = new List<RecipeStep>();
TextReader textReader = new StringReader(recipeText);
var recipeTree = XElement.Load(textReader, LoadOptions.PreserveWhitespace);
textReader.Close();
foreach (var element in recipeTree.Elements()) { foreach (var element in recipeTree.Elements()) {
// Recipe mETaDaTA // Recipe mETaDaTA