eKek0
eKek0

Reputation: 23289

How do I create a detailed error page?

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?

Upvotes: 4

Views: 571

Answers (5)

Helen Toomik
Helen Toomik

Reputation: 2115

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.

Upvotes: -1

Martin Brown
Martin Brown

Reputation: 25320

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.

Upvotes: 1

RSolberg
RSolberg

Reputation: 26972

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);
    }
}

Upvotes: 3

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

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.

Upvotes: 0

Jakob Christensen
Jakob Christensen

Reputation: 14956

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

Upvotes: 0

Related Questions