From 1635382621d78f09dd6d85e51ab146106a2cfbce Mon Sep 17 00:00:00 2001 From: Dred95 Date: Thu, 13 Oct 2016 01:43:23 +0600 Subject: [PATCH] Added more informative exception messages Print name, tag and type in exceptions --- .../UnityBinding/UnityBindingExtension.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Assets/Adic/Scripts/Extensions/UnityBinding/UnityBindingExtension.cs b/src/Assets/Adic/Scripts/Extensions/UnityBinding/UnityBindingExtension.cs index 8ab8b3a..73c005d 100644 --- a/src/Assets/Adic/Scripts/Extensions/UnityBinding/UnityBindingExtension.cs +++ b/src/Assets/Adic/Scripts/Extensions/UnityBinding/UnityBindingExtension.cs @@ -16,8 +16,12 @@ public static class UnityBindingExtension { "The type must be derived from UnityEngine.Component."; private const string GAMEOBJECT_IS_NULL = "There's no GameObject to bind the type to."; - private const string PREFAB_IS_NULL = - "There's no prefab to bind the type to."; + private const string GAMEOBJECT_NAME_TYPE_IS_NULL = + "There's no GameObject named \"{0}\" to bind the type {1} to."; + private const string GAMEOBJECT_TAG_TYPE_IS_NULL = + "There's no GameObject tagged \"{0}\" to bind the type {1} to."; + private const string PREFAB_NAME_TYPE_IS_NULL = + "There's no prefab named \"{0}\" to bind the type {1} to."; private const string RESOURCE_IS_NULL = "There's no resource to bind the type to."; @@ -142,7 +146,10 @@ public static IBindingConditionFactory ToGameObject(this IBindingFactory binding } else { gameObject = GameObject.Find(name); } - + + if (gameObject == null) { + throw new BindingException(string.Format(GAMEOBJECT_NAME_TYPE_IS_NULL, name, type.ToString())); + } return CreateSingletonBinding(bindingFactory, gameObject, type, isGameObject); } @@ -223,6 +230,10 @@ public static IBindingConditionFactory ToGameObjectWithTag(this IBindingFactory var gameObject = GameObject.FindWithTag(tag); + if (gameObject == null) { + throw new BindingException(string.Format(GAMEOBJECT_TAG_TYPE_IS_NULL, tag, type.ToString())); + } + return CreateSingletonBinding(bindingFactory, gameObject, type, isGameObject); } @@ -376,7 +387,7 @@ public static IBindingConditionFactory ToPrefab(this IBindingFactory bindingFact var prefab = Resources.Load(name); if (prefab == null) { - throw new BindingException(PREFAB_IS_NULL); + throw new BindingException(string.Format(PREFAB_NAME_TYPE_IS_NULL, name, type.ToString())); } var prefabBinding = new PrefabBinding(prefab, type); @@ -451,7 +462,7 @@ public static IBindingConditionFactory ToPrefabSingleton(this IBindingFactory bi var prefab = Resources.Load(name); if (prefab == null) { - throw new BindingException(PREFAB_IS_NULL); + throw new BindingException(string.Format(PREFAB_NAME_TYPE_IS_NULL, name, type.ToString())); } var gameObject = (GameObject)MonoBehaviour.Instantiate(prefab); @@ -512,4 +523,4 @@ private static IBindingConditionFactory CreateSingletonBinding(IBindingFactory b } } } -} \ No newline at end of file +}