Skip to content

Commit dadc47d

Browse files
authored
Merge pull request #6699 from Troy-Ferrell/users/trferrel/eventhandler-try-catch
Don't crash on global listeners
2 parents c693813 + 4efade7 commit dadc47d

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Assets/MixedRealityToolkit.Tests/PlayModeTests/Components/TestInputGlobalListener.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using Microsoft.MixedReality.Toolkit.Input;
67
using Microsoft.MixedReality.Toolkit.Utilities;
@@ -93,7 +94,7 @@ public void OnPointerUp(MixedRealityPointerEventData eventData)
9394
pointerUpCount++;
9495
}
9596

96-
public void OnPointerClicked(MixedRealityPointerEventData eventData)
97+
public virtual void OnPointerClicked(MixedRealityPointerEventData eventData)
9798
{
9899
pointerClickedCount++;
99100
}
@@ -104,6 +105,15 @@ public void OnSpeechKeywordRecognized(SpeechEventData eventData)
104105
}
105106
}
106107

108+
internal class TestInputGlobalListenerException : TestInputGlobalListener
109+
{
110+
public const string ExceptionMessage = "Test exception thrown during event fired for global listener";
111+
public override void OnPointerClicked(MixedRealityPointerEventData eventData)
112+
{
113+
throw new Exception(ExceptionMessage);
114+
}
115+
}
116+
107117
internal class TestInputGlobalListenerObjectBased : TestInputGlobalListener
108118
{
109119
TestInputGlobalListenerObjectBased()

Assets/MixedRealityToolkit.Tests/PlayModeTests/InputEventSystemTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,30 @@ public IEnumerator TestInputSystemGlobalHandlerListener()
344344
Object.Destroy(object1);
345345
yield return null;
346346
}
347+
348+
/// <summary>
349+
/// Test input system catches exception thrown in global listener responding to input event and that
350+
/// the input system does not crash accordingly as well
351+
/// </summary>
352+
[UnityTest]
353+
public IEnumerator TestGlobalListenerExceptionThrown()
354+
{
355+
var inputSystem = CoreServices.InputSystem;
356+
357+
var object1 = new GameObject("Object");
358+
var listener = object1.AddComponent<TestInputGlobalListenerException>();
359+
360+
yield return null;
361+
362+
// Emit pointer event, which should be received by global handler
363+
inputSystem.RaisePointerClicked(inputSystem.GazeProvider.GazePointer, MixedRealityInputAction.None, 1);
364+
365+
LogAssert.Expect(LogType.Exception, $"Exception: {TestInputGlobalListenerException.ExceptionMessage}");
366+
367+
Object.Destroy(object1);
368+
369+
yield return null;
370+
}
347371
}
348372
}
349373
#endif

Assets/MixedRealityToolkit/Services/BaseEventSystem.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ public virtual void HandleEvent<T>(BaseEventData eventData, ExecuteEvents.EventF
9494
continue;
9595
}
9696

97-
eventHandler.Invoke((T)handlerEntry.handler, eventData);
97+
try
98+
{
99+
// Ensure client code does not crash our input system
100+
eventHandler.Invoke((T)handlerEntry.handler, eventData);
101+
}
102+
catch (Exception ex)
103+
{
104+
Debug.LogException(ex);
105+
}
98106
}
99107
}
100108

0 commit comments

Comments
 (0)