This project is read-only.

Unhandled Timeouts when Retrieving Timeline

May 16, 2010 at 4:17 PM
Edited May 16, 2010 at 4:18 PM

Exceptions which can occur when retrieving the user timeline are not handled properly and can either cause your site to fail with an exception if it happens during the first synchronous call or the working process to crash with an unhandled exception if it happens during later asynchronous calls.

The problem can be fixed by adding try blocks where the exceptions are raised. The control then displays the data it has previously retrieved or an empty timeline if the exception is thrown during the first call.

Below are the modified methods in the Repository class:

 

public void GetPublicUserTimelineFromTwitter(string user, int numberOfTweets, int timeout, bool isSyncronous)
{
	if (String.IsNullOrEmpty(user)) throw new ArgumentNullException("user");
	if (numberOfTweets < 1) throw new ArgumentException("Cannot get less than 1 tweet", "numberOfTweets");
	if (timeout < 1000) throw new ArgumentException("Cannot retrieve Twitter data within less than one second", "timeout");
	HttpWebRequest request = CreateTwitterRequest(user, numberOfTweets, timeout);

	if (isSyncronous)
	{
		try
		{
			ProcessTwitterResponse((HttpWebResponse)request.GetResponse());
		}
		catch { };
	}
	else
	{
		request.BeginGetResponse(GetPublicUserTimelineJsonAsyncResultProcessor, request);
	}
}

private void GetPublicUserTimelineJsonAsyncResultProcessor(IAsyncResult ar)
{
	try
	{
		HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
		ProcessTwitterResponse((HttpWebResponse)request.EndGetResponse(ar));
	}
	catch { }
}