diff --git a/Assets/Runtime/GameEvents/Game/GameEvent.cs b/Assets/Runtime/GameEvents/Game/GameEvent.cs index 60fff24..109fe2b 100644 --- a/Assets/Runtime/GameEvents/Game/GameEvent.cs +++ b/Assets/Runtime/GameEvents/Game/GameEvent.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using GameEvents.Generic; using UnityEngine; @@ -39,7 +40,15 @@ public void RaiseGameEvent() Debug.Log($"Raise event: {name}, listener: {listener}"); } - listener.RaiseGameEvent(); + try + { + listener.RaiseGameEvent(); + } + catch (Exception e) + { + Debug.Log($"Listener: {listener} of event: {name} has thrown an exception."); + Debug.LogException(e, this); + } } } diff --git a/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs b/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs index 763da03..a89fdf0 100644 --- a/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs +++ b/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using UnityEngine; @@ -38,7 +39,15 @@ public void RaiseGameEvent(TArgument argument) Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}"); } - listener.RaiseGameEvent(argument); + try + { + listener.RaiseGameEvent(argument); + } + catch (Exception e) + { + Debug.Log($"Listener: {listener} of event: {name} has thrown an exception."); + Debug.LogException(e, this); + } } } diff --git a/Assets/Tests/Runtime/GameEvents/GameEventTest.cs b/Assets/Tests/Runtime/GameEvents/GameEventTest.cs index d6cd485..1ef7862 100644 --- a/Assets/Tests/Runtime/GameEvents/GameEventTest.cs +++ b/Assets/Tests/Runtime/GameEvents/GameEventTest.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using GameEvents.Bool; using GameEvents.Float; using GameEvents.Game; @@ -117,6 +118,40 @@ public void ShouldRaiseGameEventEvent() Assert.AreEqual(0, count[0]); } + + [Test] + public void ShouldNotBreakChainWhenExceptionIsThrown() + { + // Given. + var gameObject = new UnityEngine.GameObject(); + gameObject.SetActive(false); + + var listenerWithError = gameObject.AddComponent(); + var listener = gameObject.AddComponent(); + + listenerWithError.OnGameEvent = new UnityEvent(); + listenerWithError.GameEvent = ScriptableObject.CreateInstance(); + + listener.OnGameEvent = new UnityEvent(); + listener.GameEvent = listenerWithError.GameEvent; + + var count = new int[1]; + listenerWithError.OnGameEvent.AddListener(() => throw new NullReferenceException()); + listener.OnGameEvent.AddListener(() => count[0]++); + + // Then. + gameObject.SetActive(true); + listener.GameEvent.RaiseGameEvent(); + + Assert.AreEqual(1, count[0]); + count[0] = 0; + + gameObject.SetActive(false); + listener.GameEvent.RaiseGameEvent(); + + Assert.AreEqual(0, count[0]); + } + [Test] public void ShouldRegisterAndUnregisterGameObjectGameEventListener() {