From dd98e9c2dface0a190c34914af06d4d9dd3916ea Mon Sep 17 00:00:00 2001 From: KacperKenjiLesniak Date: Fri, 18 Dec 2020 20:37:30 +0100 Subject: [PATCH 1/2] Wrapped notifying listeners in a try catch --- Assets/Runtime/GameEvents/Game/GameEvent.cs | 15 +++++++- .../GameEvents/Generic/ArgumentGameEvent.cs | 15 +++++++- .../Tests/Runtime/GameEvents/GameEventTest.cs | 37 ++++++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Assets/Runtime/GameEvents/Game/GameEvent.cs b/Assets/Runtime/GameEvents/Game/GameEvent.cs index 60fff24..16c5337 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,17 @@ public void RaiseGameEvent() Debug.Log($"Raise event: {name}, listener: {listener}"); } - listener.RaiseGameEvent(); + try + { + listener.RaiseGameEvent(); + } + catch (Exception e) + { + if (debug) + { + Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}"); + } + } } } diff --git a/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs b/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs index 763da03..44951b5 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,17 @@ public void RaiseGameEvent(TArgument argument) Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}"); } - listener.RaiseGameEvent(argument); + try + { + listener.RaiseGameEvent(argument); + } + catch (Exception e) + { + if (debug) + { + Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}"); + } + } } } 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() { From 94aa5e7c463b0ef558080714dfa8cbeb79cacf22 Mon Sep 17 00:00:00 2001 From: KacperKenjiLesniak Date: Sun, 17 Jan 2021 16:02:03 +0100 Subject: [PATCH 2/2] Review changes --- Assets/Runtime/GameEvents/Game/GameEvent.cs | 6 ++---- Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Assets/Runtime/GameEvents/Game/GameEvent.cs b/Assets/Runtime/GameEvents/Game/GameEvent.cs index 16c5337..109fe2b 100644 --- a/Assets/Runtime/GameEvents/Game/GameEvent.cs +++ b/Assets/Runtime/GameEvents/Game/GameEvent.cs @@ -46,10 +46,8 @@ public void RaiseGameEvent() } catch (Exception e) { - if (debug) - { - Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}"); - } + 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 44951b5..a89fdf0 100644 --- a/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs +++ b/Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs @@ -45,10 +45,8 @@ public void RaiseGameEvent(TArgument argument) } catch (Exception e) { - if (debug) - { - Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}"); - } + Debug.Log($"Listener: {listener} of event: {name} has thrown an exception."); + Debug.LogException(e, this); } } }