Skip to content

Instantly share code, notes, and snippets.

@akshaysura
Last active June 12, 2025 19:44
Show Gist options
  • Save akshaysura/c5802500c505006662068ed46f8e708c to your computer and use it in GitHub Desktop.
Save akshaysura/c5802500c505006662068ed46f8e708c to your computer and use it in GitHub Desktop.
CatchErrorsRendererErrorStrategy
using System;
using System.IO;
using System.Web.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.Abstractions;
using Sitecore.DependencyInjection;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;
using Sitecore;
using Sitecore.XA.Foundation.Multisite.Extensions;
namespace SOMESOLUTION.SOMEPROJECT.PipelineHandlers
{
public class CatchErrorsRendererErrorStrategy : PageModeRenderingErrorStrategy, IRendererErrorStrategy
{
public virtual BaseLog Logger { get { return ServiceLocator.ServiceProvider.GetService<BaseLog>(); } }
public virtual bool IsNormalMode { get { return Sitecore.Context.PageMode.IsNormal; } }
public virtual new bool HandleError(Renderer renderer, Exception ex, TextWriter writer)
{
//log an error in the log file regardless
this.Logger.Error("Rendering Errored:", ex, typeof(CatchAllRendererErrorStrategy));
//exit if it is not an SXA site
if (!Context.Site.IsSxaSite())
{
return false;
}
//handle it if it is not in the normal mode, assuming in the experience editor
if (!this.IsNormalMode)
{
var container = new TagBuilder("div");
container.Attributes.Add("class", "sxa-rendering-error");
container.Attributes.Add("style", "background-color: #f4b2ae;");
TagBuilder hr = new TagBuilder("hr");
container.InnerHtml += hr.ToString();
container.InnerHtml += "The rendering failed to render. Please check the datasource or rendering parameters. (This message is only displayed in the editing interface)" + renderer.CacheKey + ex.Message;
container.InnerHtml += hr.ToString();
writer.Write(container.ToString());
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment