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

Commit 162dd14

Browse files
authored
Merge pull request #29 from KacperKenjiLesniak/issue-28
#28 Added try-catch clause
2 parents 5ab7c99 + 94aa5e7 commit 162dd14

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Assets/Runtime/GameEvents/Game/GameEvent.cs

Lines changed: 11 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,15 @@ 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+
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception.");
50+
Debug.LogException(e, this);
51+
}
4352
}
4453
}
4554

Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs

Lines changed: 11 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,15 @@ 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+
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception.");
49+
Debug.LogException(e, this);
50+
}
4251
}
4352
}
4453

Assets/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)