1
votes

We have an image upload page that will timeout if the user's upload is taking longer than 15 minutes.

We're catching the HttpException that occurs with a timeout. But how can we know that the exception occurred because of a timeout, so we can return a specific message?

Our code:

try
{
    // do stuff here
}
catch (HttpException ex)
{
    // What can we check to know if this is a timeout exception?
    // if (ex == TimeOutError)
    // {
    //      return "Took too long. Please upload a smaller image.";
    // }
    return "Error with image. Try again.";
}
catch (Exception ex)
{
    return "Error with image. Try again.";
}

And what the timeout error looks like:

System.Web.HttpException (0x80004005): Request timed out.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.GetMultipartContent()
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.EnsureForm()
at System.Web.HttpRequest.get_Form()
at MyStore.upload.ProcessRequest(HttpContext context) 

ex.ErrorCode=-2147467259
ex.GetHttpCode=500
ex.WebEventCode=0

I'm hesitant to simply do an if statement that compares the error codes above.

HttpCode 500 seems to be a generic Internal Server Error code that could happen for more than just a timeout exception.

ErrorCode -2147467259 is something I'm unfamiliar with. If that number will remain constant for timeout errors, and will never occur with non-timeout exceptions, then I could do an if comparison on this number.

I'm thinking there has to be a simple way to know if the HttpException is a timeout exception, ala something like:

if (ex == TimeoutError) // what should this be?

UPDATE:

I just now tried catching TimeoutException, like the following, but it still is only caught by the HttpException.

try
{
    // do stuff here
}
catch (TimeoutException ex)
{
    // Timeout doesn't get caught. Must be a different type of timeout.
    // So far, timeout is only caught by HttpException.
    return "Took too long. Please upload a smaller image.";
}
catch (HttpException ex)
{
    // What can we check to know if this is a timeout exception?
    // if (ex == TimeOutError)
    // {
    //      return "Took too long. Please upload a smaller image.";
    // }
    return "Error with image. Try again.";
}
catch (Exception ex)
{
    return "Error with image. Try again.";
}
1
ex typeof TimeoutException - Ya Wang
@YaWang You should put that as an answer. - Doug S

1 Answers

-1
votes

You need to use

ex.getType() is subClassException

As TimeoutException is a subclass. That would be how to catch that type of execption if it was a possible throw...

Although, httpexpection will always be thrown even if it did time out (refer https://msdn.microsoft.com/en-us/library/system.web.httprequest(v=vs.110).aspx from what each method throws). So you need to do something like,

if(e.Message.Contains("timed out"))
   //Do something because it timed out