I’ve been trying to catch and/or hide this “Curl error 28: Operation timed out after 1000 milliseconds with 0 bytes received”. I’m using UnityWebRequest and I’m able to handle a timeout but the error shows up in logs and console anyways, I’ve tried with both Coroutines and just iterating the UnityWebRequestAsyncOperation but I can’t find where I could catch that ugly error.
using UnityEngine;
using UnityEngine.Networking;
public class RestClientTest : MonoBehaviour
{
UnityWebRequest Request;
UnityWebRequestAsyncOperation ActiveRequest;
void Start()
{
MakeRequest();
}
private void Update()
{
UpdateActiveRequest();
}
private void MakeRequest()
{
Request = UnityWebRequest.Get("http://localhost:8081/api/");
ActiveRequest = Request.SendWebRequest();
}
private void UpdateActiveRequest()
{
if (ActiveRequest.isDone)
{
Debug.Log("Request is done!");
if (Request.isNetworkError || Request.isHttpError)
{
Debug.Log("Request has error");
}
}
else
{
Debug.Log("Request is at " +
(ActiveRequest.progress * 100f).ToString("F2") + "%");
}
}
}
This is an error Unity internal logs. You web request should come out as error, the message is logged for help (in some cases in contains some useful info).
Your code sample does not contain timeout, yet the error says it timed out after one second, which is VERY small timeout.
Kurt: yeah I definitely get the error from the UnityWebRequest but when you select the error you don’t get the usual info such as callstack or the line that originated the error.
Aurimas: you are right, I forgot to add the line where I’m adding a timeout. Turns out everything works much better when I don’t set a timeout even when I use an invalid or non-responsive address.
I will continue just using everything without a timeout but it would definitely be nice if those Curl errors could be caught and hidden, I understand they can be useful but you don’t always want to see them.
Hitting this myself. I have an admin app that pings 10 other IP addresses to check status. Responses don’t need to be immediate. I’m testing to see if the computers are present, a step above pinging them. The system works, but if the computer at the IP address is offline it times out and I get the Curl error.
The error must be triggered by a separate thread because there is no information about processes. Just the error on a single line. If I don’t set the timeout to a second or two the app will wait a full 30 seconds before reporting the error.
I’m guessing that these kinds of errors just get passed through.
This is kinda ridiculous, but seems to solve it. It basically abort the request right before the timeout expires. I would suggest a timeout of at least 5 seconds.
public static async Task<bool> SendRequest(UnityWebRequest request)
{
int wait = 0;
int wait_max = request.timeout * 1000;
request.timeout += 1; //Add offset to make sure it abort first
var asyncOp = request.SendWebRequest();
while (!asyncOp.isDone)
{
await Task.Delay(200);
wait += 200;
if (wait >= wait_max)
request.Abort();
}
return request.result == UnityWebRequest.Result.Success;
}
I would strongly suggest Unity to change this error into a log or warning in future versions.