Mercurial > repos > IBBoard.WarFoundry.API
changeset 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 | 65553d2c8612 |
children | a1a6b527cd70 |
files | api/Factories/Xml/WarFoundryXmlFactoryUtils.cs api/Factories/Xml/WarFoundryXmlRaceFactory.cs |
diffstat | 2 files changed, 11 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactoryUtils.cs Sun Nov 22 20:14:51 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactoryUtils.cs Sat Nov 28 16:06:06 2009 +0000 @@ -80,7 +80,7 @@ private static void ValidationEventMethod(object sender, ValidationEventArgs e) { - throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); + throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); } private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation)
--- 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;