2

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.";
}
Doug S
  • 10,146
  • 3
  • 40
  • 45

2 Answers2

0

You can catch a couple timeout conditions with:

    bool IsTimeout(HttpException httpex)
    {

        switch ((HttpStatusCode)httpex.GetHttpCode())
        {
            case HttpStatusCode.RequestTimeout:    //408
            case HttpStatusCode.GatewayTimeout:   //504
                return true;
            default: return false;
        }
    }
  • Unfortunately, as mentioned in the question, the HttpCode returned by the exception is `500`. HttpCode `500` seems to be a generic `Internal Server Error` code that could happen for more than just a timeout exception. – Doug S Feb 10 '22 at 13:07
-1

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
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • Doesn't compile. If this is VB code, please answer with C#. When a question has a language specific tag, in this case `C#`, that's the language they're asking for. – Doug S Nov 05 '15 at 18:27
  • Your edit still doesn't compile. I assume you meant the code to be `ex.GetType() is TimeoutException`. Notice the uppercase `GetType()` and actual `TimeoutException`. At any rate, even the fixed code doesn't work. Compile warning: `The given expression is never of the provided ('System.TimeoutException') type`. – Doug S Nov 05 '15 at 18:50
  • @DougS I see your problem... As even though it did get timed out it didn't throw an TimeoutException. And It never does it always only throws an HTTP Exception in order to tell you need to get the message of the exeption and do .Contains on the string message – Ya Wang Nov 05 '15 at 19:48
  • Man, it'd be ugly to have to do a `e.Message.Contains("timed out")` to know whether it may be a timeout error. Hopefully there is a better way. It seems there would have to be. I'll let this question sit for awhile, and continue to research it myself, to see if there's a better solution. Thanks for your ideas. – Doug S Nov 05 '15 at 23:46
  • Well as mentioned in the question, the returned HttpCode `500` seems to be just a generic `Internal Server Error`, which could occur with many types of errors. The other ErrorCode of `-2147467259` I haven't been able to find any info on, or whether it's solely for timeout errors. – Doug S Nov 06 '15 at 01:32