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.
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>







Comments
Post a Comment