From fa6958ed953d5dbbb399bb122628015840c3a294 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Thu, 22 Aug 2019 15:20:10 -0700 Subject: [PATCH] more helpful ID lookup error messages --- encompass-cs/ComponentManager.cs | 27 ++++++++++++++++--- encompass-cs/ComponentMessageManager.cs | 27 ++++++++++++++++--- encompass-cs/EntityManager.cs | 11 +++++++- .../Exceptions/ComponentNotFoundException.cs | 12 +++++++++ .../Exceptions/EntityNotFoundException.cs | 12 +++++++++ 5 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 encompass-cs/Exceptions/ComponentNotFoundException.cs create mode 100644 encompass-cs/Exceptions/EntityNotFoundException.cs diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 3f9371f..53cdbf0 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -167,17 +167,38 @@ namespace Encompass internal IComponent GetComponentByID(Guid componentID) { - return IDToComponent[componentID]; + if (IDToComponent.ContainsKey(componentID)) + { + return IDToComponent[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } internal Type GetComponentTypeByID(Guid componentID) { - return componentIDToType[componentID]; + if (componentIDToType.ContainsKey(componentID)) + { + return componentIDToType[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } internal Guid GetEntityIDByComponentID(Guid componentID) { - return componentIDToEntityID[componentID]; + if (componentIDToEntityID.ContainsKey(componentID)) + { + return componentIDToEntityID[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } internal void MarkAllComponentsOnEntityForRemoval(Guid entityID) diff --git a/encompass-cs/ComponentMessageManager.cs b/encompass-cs/ComponentMessageManager.cs index 029d11a..d363e37 100644 --- a/encompass-cs/ComponentMessageManager.cs +++ b/encompass-cs/ComponentMessageManager.cs @@ -277,17 +277,38 @@ namespace Encompass internal IComponent GetComponentByID(Guid componentID) { - return componentIDToComponent[componentID]; + if (componentIDToComponent.ContainsKey(componentID)) + { + return componentIDToComponent[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } internal Type GetComponentTypeByID(Guid componentID) { - return componentIDToType[componentID]; + if (componentIDToType.ContainsKey(componentID)) + { + return componentIDToType[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } internal Guid GetEntityIDByComponentID(Guid componentID) { - return componentIDToEntityID[componentID]; + if (componentIDToEntityID.ContainsKey(componentID)) + { + return componentIDToEntityID[componentID]; + } + else + { + throw new ComponentNotFoundException("Component with ID {0} does not exist.", componentID); + } } } } diff --git a/encompass-cs/EntityManager.cs b/encompass-cs/EntityManager.cs index 107918f..6a6e525 100644 --- a/encompass-cs/EntityManager.cs +++ b/encompass-cs/EntityManager.cs @@ -1,5 +1,7 @@ +using System.Linq; using System; using System.Collections.Generic; +using Encompass.Exceptions; namespace Encompass { @@ -35,7 +37,14 @@ namespace Encompass public Entity GetEntity(Guid id) { - return IDToEntity[id]; + if (IDToEntity.ContainsKey(id)) + { + return IDToEntity[id]; + } + else + { + throw new EntityNotFoundException("Entity with ID {0} does not exist.", id); + } } public void MarkForDestroy(Guid entityID) diff --git a/encompass-cs/Exceptions/ComponentNotFoundException.cs b/encompass-cs/Exceptions/ComponentNotFoundException.cs new file mode 100644 index 0000000..5f1a988 --- /dev/null +++ b/encompass-cs/Exceptions/ComponentNotFoundException.cs @@ -0,0 +1,12 @@ +using System; + +namespace Encompass.Exceptions +{ + public class ComponentNotFoundException : Exception + { + public ComponentNotFoundException( + string format, + params object[] args + ) : base(string.Format(format, args)) { } + } +} diff --git a/encompass-cs/Exceptions/EntityNotFoundException.cs b/encompass-cs/Exceptions/EntityNotFoundException.cs new file mode 100644 index 0000000..c388a12 --- /dev/null +++ b/encompass-cs/Exceptions/EntityNotFoundException.cs @@ -0,0 +1,12 @@ +using System; + +namespace Encompass.Exceptions +{ + public class EntityNotFoundException : Exception + { + public EntityNotFoundException( + string format, + params object[] args + ) : base(string.Format(format, args)) { } + } +}