await UnityWebRequestAsyncOperation throws UnityWebRequestException when isHttpError or isNetworkError

master
neuecc 2020-06-15 15:23:07 +09:00
parent a2783d3c8a
commit 00a1be8666
5 changed files with 107 additions and 17 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 013a499e522703a42962a779b4d9850c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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();