Handle 500 Exception in the Application

HttpBeginRequest Pipeline

The processors in the HttpBeginRequest pipeline generate and populate the Sitecore.Context class.

In my Application, We have the httpRequestBegin Pipeline with 30 processors.


When the httpRequestBegin pipeline is executed, the processors defined above run in order from first to last.

Handle the 500 Exception in web pages

Currently I can see the errors on my page. It’s not good to show to our live site.


Create a class file

 public class Handle500Exception: ExceptionProcessor

    {

        public override void Process(ExceptionArgs args)

        {

            try

            {

                ExceptionContent objex = new ExceptionContent();

                HttpContext httpContext = HttpContext.Current;

                httpContext.Server.ClearError();

                httpContext.Response.TrySkipIisCustomErrors = true;

                httpContext.Response.StatusCode = 500;

                httpContext.Response.Write(objex.GenerateExceptionPage());

            }

            catch (Exception ex)

            {

                Log.Info(string.Format("Process  -- error message -- {0}  stacktrace {1} ", ex.Message.ToString(), ex.StackTrace.ToString()), ex);

            }

        }

    }

 

    public class ExceptionContent

    {

        public string GenerateExceptionPage()

        {

            var context = System.Web.HttpContext.Current;

            string content = string.Empty;

            try

            {

                string domain = context.Request.Url.GetComponents(UriComponents.Scheme | UriComponents.Host, UriFormat.Unescaped);

                string errorPageLink = "/500";

 

                if (!string.IsNullOrEmpty(errorPageLink))

                {

                    content = Sitecore.Web.WebUtil.ExecuteWebPage(string.Concat(domain, errorPageLink));

                    Log.Error("Internal server error happened: " + context.Request.Url, this);

                }

            }

            catch (Exception ex)

            {

                Sitecore.Diagnostics.Log.Error("Error happened while generating ExceptionPage for 500 Error  :" + ex.Message, this);

                return string.Empty;

            }

            return content;

        }

 

    }

Create a config file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">

  <sitecore>

    <pipelines>

      <mvc.exception>

        <processor type="Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage, Sitecore.Mvc">

          <patch:attribute name="type">Sitecon.Project.CustomPipeline.Pipeline.Handle500Exception, Sitecon.Project.CustomPipeline</patch:attribute>

        </processor>

      </mvc.exception>

    </pipelines>

  </sitecore>

</configuration>

Create New 500 Page in Sitecore



Before publishing check the ShowConfig File 



After Publishing the class and config files to sitecore instance


Above config file has been overridden by my new config.


Try to hit the page Again



Sorry, I forget to publish my 500 pages to web database.














Comments

Popular posts from this blog

Email Experience Manager (EXM)

Federated Experience Manager (FXM)