From 8eac07ad247bca518224cbdb9d1828ede4864b7c Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 1 Sep 2023 10:37:46 +0900 Subject: [PATCH] Use innerException for Task.AsUniTask --- .../TaskExtensionsTest.cs | 40 +++++++++++++++++++ .../UniTask/Runtime/UniTaskExtensions.cs | 4 +- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/UniTask.NetCoreTests/TaskExtensionsTest.cs diff --git a/src/UniTask.NetCoreTests/TaskExtensionsTest.cs b/src/UniTask.NetCoreTests/TaskExtensionsTest.cs new file mode 100644 index 0000000..845a062 --- /dev/null +++ b/src/UniTask.NetCoreTests/TaskExtensionsTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; +using Cysharp.Threading.Tasks; +using Xunit; + +namespace NetCoreTests +{ + public class TaskExtensionsTest + { + [Fact] + public async Task PropagateException() + { + await Assert.ThrowsAsync(async () => + { + await ThrowAsync().AsUniTask(); + }); + + await Assert.ThrowsAsync(async () => + { + await ThrowOrValueAsync().AsUniTask(); + }); + + + await Assert.ThrowsAsync(async () => + { + await Task.WhenAll(ThrowAsync(), ThrowAsync(), ThrowAsync()); + }); + } + + async Task ThrowAsync() + { + throw new InvalidOperationException(); + } + + async Task ThrowOrValueAsync() + { + throw new InvalidOperationException(); + } + } +} diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTaskExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTaskExtensions.cs index d330109..77ce536 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTaskExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UniTaskExtensions.cs @@ -28,7 +28,7 @@ namespace Cysharp.Threading.Tasks p.TrySetCanceled(); break; case TaskStatus.Faulted: - p.TrySetException(x.Exception); + p.TrySetException(x.Exception.InnerExceptions.Count == 1 ? x.Exception.InnerException : x.Exception); break; case TaskStatus.RanToCompletion: p.TrySetResult(x.Result); @@ -58,7 +58,7 @@ namespace Cysharp.Threading.Tasks p.TrySetCanceled(); break; case TaskStatus.Faulted: - p.TrySetException(x.Exception); + p.TrySetException(x.Exception.InnerExceptions.Count == 1 ? x.Exception.InnerException : x.Exception); break; case TaskStatus.RanToCompletion: p.TrySetResult();