-
Notifications
You must be signed in to change notification settings - Fork 50
Open
Description
First: Thanks for building this!
I was having inconsistencies when using the latest pull. I realized it was related to two issues:
First: the try/catch blocks in httpHandler are actually consuming the exception and acting like everything is fine. An example proposed structure would look something closer to
public static async Task<UnityWebRequest> GetAsync(string url, string authToken = null){
UnityWebRequest request = UnityWebRequest.Get(url);
if (!String.IsNullOrEmpty(authToken))
request.SetRequestHeader("Authorization", "Bearer " + authToken);
OnRequestBegin requestBegin = new OnRequestBegin();
requestBegin.FireEvent();
try{
await request.SendWebRequest();
OnRequestEnded requestSucceeded = new OnRequestEnded(request.downloadHandler.text);
requestSucceeded.FireEvent();
return request;
}
catch(Exception e){
Debug.Log("Testing exceptions");
OnRequestEnded requestEnded = new OnRequestEnded(e);
requestEnded.FireEvent();
throw;
}
}
Also your async/await library has a nullref bug in it....somewhere. Rather than debugging it - I tried using an extension method I found somewhere that's been super stable (been using it for years). Everything started showing up just fine after that. Feel free to steal it if you like:
public class UnityWebRequestAwaiter : INotifyCompletion
{
private UnityWebRequestAsyncOperation asyncOp;
private Action continuation;
public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
{
this.asyncOp = asyncOp;
asyncOp.completed += OnRequestCompleted;
}
public bool IsCompleted { get { return asyncOp.isDone; } }
public void GetResult() { }
public void OnCompleted(Action continuation)
{
this.continuation = continuation;
}
private void OnRequestCompleted(AsyncOperation obj)
{
continuation();
}
}
public static class ExtensionMethods
{
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
{
return new UnityWebRequestAwaiter(asyncOp);
}
}
cheers!
sidneyaraujomelo, dlorddd, DerekNien, Drech00T and MaksimNarchuk
Metadata
Metadata
Assignees
Labels
No labels