Surendra Sharma

Surendra Sharma

Search This Blog

Thursday, April 23, 2015

How to create error page in Sitecore


In every Sitecore application, you have to create error page. Error can be occurred at any time anywhere in application [We developers are always right. Errors are always due to incorrect contents entered by content editor J].

Anyway here are the steps to implement it

·         Create template, layout and content item for error page. I am creating only two fields in template here – Error Title, Error Message
·         On error page, I am displaying information of both these fields entered by content tree.
·         Generally I am creating single layout for all the contents. So I implemented the Page_Error event to capture all the error generated at layout page level.
·         Here is code

/// <summary>
/// page-level exception handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Page_Error(object sender, EventArgs e)
{
    // retrieve the last exception
    Exception exception = HttpContext.Current.Server.GetLastError();

    // handle any exception
    if (exception != null)
    {
        string error = exception.ToString();
        HttpContext.Current.Server.ClearError();
        ErrorLog(error);
    }
}

/// <summary>
/// Log and redirect to error page
/// </summary>
/// <param name="error"></param>
private void ErrorLog(string error)
{
    // log and clear the exception
    Sitecore.Diagnostics.Log.Error(error, this);
    // Pass the error on to the Generic Error page   
    string errorPageURL = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Database.Items.GetItem(new ID("{SSSS778C-JKHJ-SKSK-A01C-411E2028KKKK}")));
    Response.Redirect(errorPageURL);
}


Please leave your comments or share this code if it’s useful for you.

No comments:

Post a Comment