views:

214

answers:

5

I want to create a custom error page to handle all unhandled exceptions.

I want to redirect to it from the Application_Error(object sender, EventArgs e) method located in Global.asax.cs. How can I show in it some detailed information from the exception which throws the application error?

A: 

You can use Server.GetLastError() to get the exception thrown by your application.

Jakob Christensen
A: 

In your page, do something like this:

ErrorLabel.Text = Server.GetLastError();

This assumes C#. You can then redirect from the global.asax file ApplicationError event. There are other ways of handling this, and I do not recommend using the exception message to show the user.

Gregory A Beamer
+3  A: 

I've done the same thing you are talking about. I created an ErrorPage that displays info to the user. I also created a function that writes the error info into the Event Logs...

For the page, this is what I'm doing. Just put the labels on there somewhere...

protected void Page_Load(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();
    this.lblMessage.Text = ex.Message;
    this.lblSource.Text = ex.Source;
    this.lblStackTrace.Text = ex.StackTrace;
    if (AppProperties.AppEnv != AppEnvironment.PROD)
    {
        this.ErrorDetails.Visible = true;
    }
    else
    {
        this.ErrorDetails.Visible = false;
    }
    Utility.LogError();
    Server.ClearError();
}

This is what the LogError function looks like...

public static void LogError()
{
    LogError(HttpContext.Current.Server.GetLastError().GetBaseException());
}

public static void LogError(Exception ex)
{
    EventLog log = new EventLog();
    if (ex != null)
    {
        log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
        StringBuilder sErrorMessage = new StringBuilder();
        if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
        {
            sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
        }
        sErrorMessage.Append(ex.ToString());
        log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
    }
}
RSolberg
Should I think that AppProperties and AppEnvironment are a custom static classes? Thank you for the answers.
eKek0
Yes, sorry... AppProperties is a static class that contains a few different things such as my operating environment. I show/hide info based upon the environment...
RSolberg
+1  A: 

You can get the last exception from Server.GetLastError(). Once you have handled the error you can clear it if you want by calling Server.ClearError().

By the way it is considered bad practice to show too much to the end user can prove to be a security vulnerability. Also, be warned, if you redirect rather than returning a 500 HTTP error code various bots will not realise they are causing a crash and keep running the same broken requests on your site. So be sure to use Server.Transfer() rather than Response.Redirect() and set Response.StatusCode = 500.

Martin Brown
A: 

It is almost always a bad idea to show detailed error information to end users. You could end up exposing all sorts of unsuitable information: file names, database credentials, implementation details etc. Log the exception to a safe place (log file, database etc) but do not show it to the user.

Helen Toomik