Created
January 13, 2021 04:06
-
-
Save klpatil/e1e892638a07ae5103bf14a01e4db8a2 to your computer and use it in GitHub Desktop.
SXA Rendering Exception Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
using Sitecore; | |
using Sitecore.Abstractions; | |
using Sitecore.DependencyInjection; | |
using Sitecore.Diagnostics; | |
using Sitecore.Mvc.Pipelines.Response.RenderRendering; | |
using Sitecore.Mvc.Presentation; | |
using Sitecore.XA.Foundation.Abstractions.Configuration; | |
using Sitecore.XA.Foundation.Multisite.Extensions; | |
using Sitecore.XA.Foundation.Presentation; | |
using System; | |
using System.IO; | |
using System.Web.Mvc; | |
namespace SCBasics.Foundation.SitecoreExtensions.Pipelines.RenderRendering | |
{ | |
public class CustomPublishModeRenderingErrorStrategy : PageModeRenderingErrorStrategy, IRendererErrorStrategy | |
{ | |
private readonly BaseLog _log; | |
public CustomPublishModeRenderingErrorStrategy(BaseLog log) | |
{ | |
_log = log; | |
} | |
protected override string GetExceptionDetails(string prefix, string message, string stackTrace) | |
{ | |
string pageModeRenderingErrorClass = ServiceProviderServiceExtensions.GetService<IConfiguration<PresentationConfiguration>>(ServiceLocator.ServiceProvider).GetConfiguration().PageModeRenderingErrorClass; | |
return this.GetPreCode(stackTrace); | |
} | |
public bool HandleError(Renderer renderer, Exception ex, TextWriter writer) | |
{ | |
bool isCustomErrorHandlerEnabled = Sitecore.Configuration.Settings.GetBoolSetting("SCBasics.IsCustomErrorEnabled", true); | |
if (!isCustomErrorHandlerEnabled) | |
return false; | |
/* | |
* Sitecore.XA.Foundation.Presentation.Pipelines.RenderRendering.SxaPageModeRenderingErrorStrategy | |
* NON SXA References: | |
* https://www.teamdevelopmentforsitecore.com/Blog/robust-mvc-rendering-exception-handler | |
* https://www.sitecorenutsbolts.net/2015/10/23/Rendering-Exception-Handling-The-Right-Way/ | |
*/ | |
if (this.IsPageModeNormal()) | |
{ | |
return PublishModeHandleError(renderer, ex, writer); | |
} | |
if (!Context.Site.IsSxaSite()) | |
{ | |
return false; | |
} | |
return base.HandleError(renderer, ex, writer); | |
} | |
private bool PublishModeHandleError(Renderer renderer, Exception ex, TextWriter writer) | |
{ | |
Assert.IsNotNull(renderer, "renderer"); | |
Assert.IsNotNull(ex, "ex"); | |
Assert.IsNotNull(writer, "writer"); | |
if (!this.IsPageModeNormal()) | |
{ | |
return false; | |
} | |
string prefix = string.Format("Error Rendering {0}: ", renderer); | |
string str3 = this.GetExceptionDetails(prefix, ex.Message, ex.StackTrace); | |
// Log Error | |
_log.Error(string.Format("Failed to render component '{0}'", prefix), ex, this); | |
// Log main error in comment | |
writer.WriteLine("<!-- Exception rendering component '{0}': {1} -->", | |
prefix + ":" + ex.Message, | |
str3); | |
// Log Inner exception in comment | |
Exception innerException = ex.InnerException; | |
string str2 = "Inner Exception: "; | |
while (innerException != null) | |
{ | |
string str4 = this.GetExceptionDetails(str2, innerException.Message, innerException.StackTrace); | |
writer.WriteLine("<!-- Inner Exception: {0} -->", innerException.Message); | |
innerException = innerException.InnerException; | |
} | |
return true; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<settings> | |
<!--Default true--> | |
<setting name="SCBasics.IsCustomErrorEnabled" value="true"/> | |
<!-- USE SERVER-SIDE REDIRECT FOR REQUEST ERRORS | |
If true, Sitecore will use Server.Transfer instead of Response.Redirect to redirect request to service pages | |
when an error occurs (item not found, access denied etc). | |
Default value: false | |
--> | |
<setting name="RequestErrors.UseServerSideRedirect" value="true"/> | |
</settings> | |
<pipelines> | |
<mvc.renderRendering> | |
<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc"> | |
<param desc="rendererErrorHandler" type="Sitecore.Mvc.Pipelines.Response.RenderRendering.HttpExceptionWrappingRendererErrorStrategy, Sitecore.Mvc"> | |
<param desc="rendererErrorHandler" type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ChainedRendererErrorStrategy, Sitecore.Mvc"> | |
<Handlers hint="list"> | |
<handler patch:after="*[@type='Sitecore.XA.Foundation.Presentation.Pipelines.RenderRendering.SxaPageModeRenderingErrorStrategy, | |
Sitecore.XA.Foundation.Presentation']" | |
type="SCBasics.Foundation.SitecoreExtensions.Pipelines.RenderRendering.CustomPublishModeRenderingErrorStrategy, | |
SCBasics.Foundation.SitecoreExtensions" resolve="true"/> | |
</Handlers> | |
</param> | |
</param> | |
</processor> | |
</mvc.renderRendering> | |
</pipelines> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i can't seem to get the processor to fire. i'm using 9.3. here is my showconfig .
here is the implementation class:
what am i missing?
thanks for you feedback,
marcos.