From 00a1be866624c1f08291d67708081455051ae3b5 Mon Sep 17 00:00:00 2001 From: neuecc Date: Mon, 15 Jun 2020 15:23:07 +0900 Subject: [PATCH] await UnityWebRequestAsyncOperation throws UnityWebRequestException when isHttpError or isNetworkError --- .../UniTask/Runtime/UnityAsyncExtensions.cs | 37 +++++++++++++++---- .../UniTask/Runtime/UnityAsyncExtensions.tt | 35 ++++++++++++++++++ .../UniTask/Runtime/UnityWebRequestError.cs | 24 ++++++++++++ .../Runtime/UnityWebRequestError.cs.meta | 11 ++++++ src/UniTask/Assets/Scenes/SandboxMain.cs | 17 ++++----- 5 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs create mode 100644 src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs.meta diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs index 9a85e37..1817d11 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.cs @@ -160,7 +160,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -466,7 +466,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -776,7 +776,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -1086,7 +1086,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -1285,12 +1285,20 @@ namespace Cysharp.Threading.Tasks continuationAction = null; var result = asyncOperation.webRequest; asyncOperation = null; + if (result.isHttpError || result.isNetworkError) + { + throw new UnityWebRequestException(result); + } return result; } else { var result = asyncOperation.webRequest; asyncOperation = null; + if (result.isHttpError || result.isNetworkError) + { + throw new UnityWebRequestException(result); + } return result; } } @@ -1367,7 +1375,15 @@ namespace Cysharp.Threading.Tasks else { completed = true; - core.TrySetResult(asyncOperation.webRequest); + var result = asyncOperation.webRequest; + if (result.isHttpError || result.isNetworkError) + { + core.TrySetException(new UnityWebRequestException(result)); + } + else + { + core.TrySetResult(result); + } } } @@ -1397,7 +1413,7 @@ namespace Cysharp.Threading.Tasks } public bool MoveNext() - { + { if (completed) { TryReturn(); @@ -1526,7 +1542,14 @@ namespace Cysharp.Threading.Tasks if (asyncOperation.isDone) { - core.TrySetResult(asyncOperation.webRequest); + if (asyncOperation.webRequest.isHttpError || asyncOperation.webRequest.isNetworkError) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } return false; } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt index 93701ef..61279a3 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.tt @@ -81,6 +81,12 @@ namespace Cysharp.Threading.Tasks <# if (!IsVoid(t)) { #> var result = <#= $"asyncOperation.{t.returnField}" #>; asyncOperation = null; +<# if(t.returnType == "UnityWebRequest") { #> + if (result.isHttpError || result.isNetworkError) + { + throw new UnityWebRequestException(result); + } +<# } #> return result; <# } else { #> asyncOperation = null; @@ -91,6 +97,12 @@ namespace Cysharp.Threading.Tasks <# if (!IsVoid(t)) { #> var result = <#= $"asyncOperation.{t.returnField}" #>; asyncOperation = null; +<# if(t.returnType == "UnityWebRequest") { #> + if (result.isHttpError || result.isNetworkError) + { + throw new UnityWebRequestException(result); + } +<# } #> return result; <# } else { #> asyncOperation = null; @@ -170,7 +182,19 @@ namespace Cysharp.Threading.Tasks else { completed = true; +<# if(t.returnType == "UnityWebRequest") { #> + var result = asyncOperation.webRequest; + if (result.isHttpError || result.isNetworkError) + { + core.TrySetException(new UnityWebRequestException(result)); + } + else + { + core.TrySetResult(result); + } +<# } else { #> core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); +<# } #> } } @@ -345,7 +369,18 @@ namespace Cysharp.Threading.Tasks if (asyncOperation.isDone) { +<# if(t.returnType == "UnityWebRequest") { #> + if (asyncOperation.webRequest.isHttpError || asyncOperation.webRequest.isNetworkError) + { + core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest)); + } + else + { + core.TrySetResult(asyncOperation.webRequest); + } +<# } else { #> core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>); +<# } #> return false; } diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs new file mode 100644 index 0000000..c5fe3ff --- /dev/null +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs @@ -0,0 +1,24 @@ +#if ENABLE_UNITYWEBREQUEST + +using System; +using UnityEngine.Networking; + +namespace Cysharp.Threading.Tasks +{ + public class UnityWebRequestException : Exception + { + public UnityWebRequest UnityWebRequest { get; } + public bool IsNetworkError { get; } + public bool IsHttpError { get; } + + public UnityWebRequestException(UnityWebRequest unityWebRequest) + : base(unityWebRequest.error + Environment.NewLine + unityWebRequest.downloadHandler.text) + { + this.UnityWebRequest = unityWebRequest; + this.IsNetworkError = unityWebRequest.isNetworkError; + this.IsHttpError = unityWebRequest.isHttpError; + } + } +} + +#endif \ No newline at end of file diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs.meta b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs.meta new file mode 100644 index 0000000..50c475e --- /dev/null +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityWebRequestError.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 013a499e522703a42962a779b4d9850c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index d9a765a..9dacea2 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -415,15 +415,12 @@ public class SandboxMain : MonoBehaviour private async UniTaskVoid ExecuteAsync() { - Debug.Log("1"); - { - var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync(); - } - Debug.Log("------------------"); - { - var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync(); - Debug.Log("2"); - } + var req = UnityWebRequest.Get("https://google.com/"); + + var v = await req.SendWebRequest().ToUniTask(); + // req.Dispose(); + Debug.Log($"{v.isDone} {v.isHttpError} {v.isNetworkError}"); + Debug.Log(v.downloadHandler.text); } @@ -446,7 +443,7 @@ public class SandboxMain : MonoBehaviour // rp.AddTo(this.GetCancellationTokenOnDestroy()); var cts = new CancellationTokenSource(); - +