This repository was archived by the owner on Aug 10, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +62
-5
lines changed Expand file tree Collapse file tree 3 files changed +62
-5
lines changed Original file line number Diff line number Diff line change 1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Collections . ObjectModel ;
34using GameEvents . Generic ;
45using 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
Original file line number Diff line number Diff line change 1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Collections . ObjectModel ;
34using 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
Original file line number Diff line number Diff line change 1- using System . Linq ;
1+ using System ;
2+ using System . Linq ;
23using GameEvents . Bool ;
34using GameEvents . Float ;
45using 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 {
You can’t perform that action at this time.
0 commit comments