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

@ -1285,12 +1285,20 @@ namespace Cysharp.Threading.Tasks
continuationAction = null; continuationAction = null;
var result = asyncOperation.webRequest; var result = asyncOperation.webRequest;
asyncOperation = null; asyncOperation = null;
if (result.isHttpError || result.isNetworkError)
{
throw new UnityWebRequestException(result);
}
return result; return result;
} }
else else
{ {
var result = asyncOperation.webRequest; var result = asyncOperation.webRequest;
asyncOperation = null; asyncOperation = null;
if (result.isHttpError || result.isNetworkError)
{
throw new UnityWebRequestException(result);
}
return result; return result;
} }
} }
@ -1367,7 +1375,15 @@ namespace Cysharp.Threading.Tasks
else else
{ {
completed = true; completed = true;
core.TrySetResult(asyncOperation.webRequest); var result = asyncOperation.webRequest;
if (result.isHttpError || result.isNetworkError)
{
core.TrySetException(new UnityWebRequestException(result));
}
else
{
core.TrySetResult(result);
}
} }
} }
@ -1526,7 +1542,14 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone) 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; return false;
} }

View File

@ -81,6 +81,12 @@ namespace Cysharp.Threading.Tasks
<# if (!IsVoid(t)) { #> <# if (!IsVoid(t)) { #>
var result = <#= $"asyncOperation.{t.returnField}" #>; var result = <#= $"asyncOperation.{t.returnField}" #>;
asyncOperation = null; asyncOperation = null;
<# if(t.returnType == "UnityWebRequest") { #>
if (result.isHttpError || result.isNetworkError)
{
throw new UnityWebRequestException(result);
}
<# } #>
return result; return result;
<# } else { #> <# } else { #>
asyncOperation = null; asyncOperation = null;
@ -91,6 +97,12 @@ namespace Cysharp.Threading.Tasks
<# if (!IsVoid(t)) { #> <# if (!IsVoid(t)) { #>
var result = <#= $"asyncOperation.{t.returnField}" #>; var result = <#= $"asyncOperation.{t.returnField}" #>;
asyncOperation = null; asyncOperation = null;
<# if(t.returnType == "UnityWebRequest") { #>
if (result.isHttpError || result.isNetworkError)
{
throw new UnityWebRequestException(result);
}
<# } #>
return result; return result;
<# } else { #> <# } else { #>
asyncOperation = null; asyncOperation = null;
@ -170,7 +182,19 @@ namespace Cysharp.Threading.Tasks
else else
{ {
completed = true; 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}" #>); core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>);
<# } #>
} }
} }
@ -345,7 +369,18 @@ namespace Cysharp.Threading.Tasks
if (asyncOperation.isDone) 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}" #>); core.TrySetResult(<#= IsVoid(t) ? "AsyncUnit.Default" : $"asyncOperation.{t.returnField}" #>);
<# } #>
return false; 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() private async UniTaskVoid ExecuteAsync()
{ {
Debug.Log("1"); var req = UnityWebRequest.Get("https://google.com/");
{
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync(); var v = await req.SendWebRequest().ToUniTask();
} // req.Dispose();
Debug.Log("------------------"); Debug.Log($"{v.isDone} {v.isHttpError} {v.isNetworkError}");
{ Debug.Log(v.downloadHandler.text);
var xs = await UniTaskAsyncEnumerable.TimerFrame(1).ToArrayAsync();
Debug.Log("2");
}
} }