Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit 1b5c251

Browse files
Wrapped notifying listeners in a try catch
1 parent 10ecf6b commit 1b5c251

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Runtime/GameEvents/Game/GameEvent.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using GameEvents.Generic;
45
using UnityEngine;
@@ -39,7 +40,17 @@ public void RaiseGameEvent()
3940
Debug.Log($"Raise event: {name}, listener: {listener}");
4041
}
4142

42-
listener.RaiseGameEvent();
43+
try
44+
{
45+
listener.RaiseGameEvent();
46+
}
47+
catch (Exception e)
48+
{
49+
if (debug)
50+
{
51+
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}");
52+
}
53+
}
4354
}
4455
}
4556

Runtime/GameEvents/Generic/ArgumentGameEvent.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using UnityEngine;
45

@@ -38,7 +39,17 @@ public void RaiseGameEvent(TArgument argument)
3839
Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}");
3940
}
4041

41-
listener.RaiseGameEvent(argument);
42+
try
43+
{
44+
listener.RaiseGameEvent(argument);
45+
}
46+
catch (Exception e)
47+
{
48+
if (debug)
49+
{
50+
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception: {e.Message}");
51+
}
52+
}
4253
}
4354
}
4455

Tests/Runtime/GameEvents/GameEventTest.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using GameEvents.Bool;
34
using GameEvents.Float;
45
using GameEvents.Game;
@@ -117,6 +118,40 @@ public void ShouldRaiseGameEventEvent()
117118
Assert.AreEqual(0, count[0]);
118119
}
119120

121+
122+
[Test]
123+
public void ShouldNotBreakChainWhenExceptionIsThrown()
124+
{
125+
// Given.
126+
var gameObject = new UnityEngine.GameObject();
127+
gameObject.SetActive(false);
128+
129+
var listenerWithError = gameObject.AddComponent<GameEventListener>();
130+
var listener = gameObject.AddComponent<GameEventListener>();
131+
132+
listenerWithError.OnGameEvent = new UnityEvent();
133+
listenerWithError.GameEvent = ScriptableObject.CreateInstance<GameEvent>();
134+
135+
listener.OnGameEvent = new UnityEvent();
136+
listener.GameEvent = listenerWithError.GameEvent;
137+
138+
var count = new int[1];
139+
listenerWithError.OnGameEvent.AddListener(() => throw new NullReferenceException());
140+
listener.OnGameEvent.AddListener(() => count[0]++);
141+
142+
// Then.
143+
gameObject.SetActive(true);
144+
listener.GameEvent.RaiseGameEvent();
145+
146+
Assert.AreEqual(1, count[0]);
147+
count[0] = 0;
148+
149+
gameObject.SetActive(false);
150+
listener.GameEvent.RaiseGameEvent();
151+
152+
Assert.AreEqual(0, count[0]);
153+
}
154+
120155
[Test]
121156
public void ShouldRegisterAndUnregisterGameObjectGameEventListener()
122157
{

0 commit comments

Comments
 (0)