Catching timeout exception in ExecuteAsync

4,260 views
Skip to first unread message

vinkaga

unread,
Aug 28, 2011, 10:26:16 PM8/28/11
to RestSharp
How does one catch the timeout exception? I tried the following 2
patterns and neither one catches the exception.

1. var request = new RestRequest(resource, Method.POST);
client.ExecuteAsync(request, response => {
try {
object result = response.Content;
callback(result);
} catch (Exception e) {
callback(e);
}
});

2. var request = new RestRequest(resource, Method.POST);
try {
client.ExecuteAsync(request, response => {
object result = response.Content;
callback(result);
});
} catch (Exception e) {
callback(e);
}

How can one catch the timeout exception? Thanks in advance.

Andrew Young

unread,
Aug 29, 2011, 2:33:33 AM8/29/11
to rest...@googlegroups.com
Timeouts don't throw exceptions. They are reported in the ResponseStatus. Check for the status in your callback.

if(response.ResponseStatus == ResponseStatus.TimedOut)
{
    // request timed out
}

vinkaga

unread,
Aug 29, 2011, 2:50:14 PM8/29/11
to RestSharp
Thanks Andrew, I added that check but I still seem to be getting an
exception (MD 2.4.2 with the latest RestSharp code from github).

https://docs.google.com/leaf?id=0B0F93HP6ltLpZTZkMTFmZGQtNDQyNy00NTk4LWEwZGUtYTM3MDYxODc5OGE3&hl=en_US

https://docs.google.com/leaf?id=0B0F93HP6ltLpOGFjYzVjMDItOTI4YS00ZTY1LWExNmItZmJlYjg5MThhN2Zk&hl=en_US

Thanks in advance.

Andrew Young

unread,
Aug 29, 2011, 3:46:05 PM8/29/11
to rest...@googlegroups.com
I think you have discovered a bug in RestSharp. 

RequestStreamCallback() is being called as soon as the request is aborted but there's no logic in there to detect if a timeout has occurred, thus a WebException is thrown when RestSharp calls EndGetRequestStream.

I wrote up an issue in github for this. https://github.com/johnsheehan/RestSharp/issues/156

But for now, is the exception being caught in your try/catch?

vinkaga

unread,
Aug 29, 2011, 4:26:43 PM8/29/11
to RestSharp
No, the exception is not being caught by try-catch on ExecuteAsync nor
by try-catch on the callback. I guess the exception is being generated
in a background thread before the callback is being called.

Andrew Young

unread,
Aug 29, 2011, 5:27:09 PM8/29/11
to rest...@googlegroups.com
If it is not catching the exception, what is your app doing when it times out? If it is crashing your app then it's probably still on the main thread. 

vinkaga

unread,
Aug 29, 2011, 6:04:05 PM8/29/11
to RestSharp
I am testing on iPhone simulator and MD stops on the exception. My
test code is running on a non-gui thread like the following but that
still stops MD.

public static void Main(string[] args) {
new Thread(new ThreadStart(ApplicationDelegate.test)).Start();
UIApplication.Main(args, null, "ApplicationDelegate");
}

public static void test() {
ping(result => {ObjectDumper.Write(result);});
}

public void ping(Action<object> callback) {
try {
var request = new RestRequest(URL_PING, Method.POST);
client.ExecuteAsync(request, response => {
try {
if (response.ResponseStatus == ResponseStatus.Completed) {
var result = response.Content;
callback(result);
} else {
callback("Request failed " + response.ResponseStatus);
}
} catch (Exception e) {
callback(e);
}
});
} catch (Exception e) {
callback(e);
}
}
Reply all
Reply to author
Forward
0 new messages