Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore logging errors for images in global asax

I have an error handler in my global.asax as follows;

  Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        Dim ex = Server.GetLastError.GetBaseException
        Dim lastErrorWrapper As HttpException = Server.GetLastError()
        Dim lastError As Exception = lastErrorWrapper

        If lastErrorWrapper.InnerException IsNot Nothing Then
            lastError = lastErrorWrapper.InnerException
        End If

        My.ErrorHandler.LogError( _
            "<BR/><BR/>URL: " & Request.RawUrl & _
            "<BR/><BR/>STACK: " & ex.StackTrace & _
            "<BR/><BR/>SOURCE: " & ex.Source & _
            "<BR/><BR/>MESSAGE: " & ex.Message & _
            "<BR/><BR/>TYPENAME: " & ex.GetType.ToString & _
            "<BR/><BR/>INNER EXCEPTION: " & lastError.ToString & _
            "<BR/><BR/>REFERRER: " & HttpContext.Current.Request.Url.AbsoluteUri & _
            "<BR/><BR/>USER IP: " & Request.ServerVariables("REMOTE_ADDR") & " -- " & Request.ServerVariables("HTTP_USER_AGENT"))
    End Sub

Obviously, this works great and sends me an email whenever there is an error. But, this is also true for any images that are not found in the file system. It gives me a "File does not exist." error. Is there a way to ignore logging errors for images that are not located on disk?

like image 962
Subliminal Hash Avatar asked Dec 18 '12 07:12

Subliminal Hash


3 Answers

Cant you resolve the FileNotFoundException instead? If the exception should not occur then rather resolve the issue than suppressing it. To handle a known exception you can use the following code.

if(Server.GetLastError().GetBaseException() is System.Web.HttpException)
{
     //You could check whether the 
     //Server.GetLastError().GetBaseException().Message contains the appropriate message
     Debug.WriteLine("Suppressed FileNotFoundException");
}else
//Log an unhandled exception
like image 157
Jonathan Avatar answered Sep 28 '22 06:09

Jonathan


var ex = Context.Error;

if (ex is HttpException)
{
    var httpException = (HttpException)ex;

    if (httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
        return; // Do nothing 404    
}
like image 41
halit Avatar answered Sep 28 '22 06:09

halit


I know that this sounds very easy, or very simple, or you may search for the error code, but this is what I do - simple check if the error contains the message of dose not exist:

lastErrorWrapper.ToString().Contains("does not exist.")
like image 40
Aristos Avatar answered Sep 28 '22 05:09

Aristos