diff api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 217:89e26d51afc2

Fixes #226: "NullReferenceException" for a unitID * Add null check and throw InvalidFileException when getting unit XML by ID * Change InvalidDataException (which is a core exception about data streams) to InvalidFileException (which is our exception for incorrect data in a file)
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 Nov 2009 16:06:06 +0000
parents 897c53d5a8aa
children f609bcf7035b
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sun Nov 22 20:14:51 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Nov 28 16:06:06 2009 +0000
@@ -92,7 +92,14 @@
 
 		private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace)
 		{
-			return GetUnitTypeForElement(WarFoundryXmlFactoryUtils.SelectSingleElement(doc, "/race:race/race:units/race:unit[@id='"+id+"']"), parentRace);
+			XmlElement unitWithId = WarFoundryXmlFactoryUtils.SelectSingleElement (doc, "/race:race/race:units/race:unit[@id='" + id + "']");
+			
+			if (unitWithId == null)
+			{
+				throw new InvalidFileException("Could not find unit with ID "+id);
+			}
+			
+			return GetUnitTypeForElement(unitWithId, parentRace);
 		}
 						
 		private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace)
@@ -138,7 +145,7 @@
 			}
 			catch (FormatException ex)
 			{
-				throw new InvalidDataException(ex.Message, ex);
+				throw new InvalidFileException(ex.Message, ex);
 			}
 
 			string mainCatID = elem.GetAttribute("cat");
@@ -146,7 +153,7 @@
 			
 			if (cat == null)
 			{
-				throw new InvalidDataException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID));
+				throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID));
 			}
 			
 			type.MainCategory = cat;