Skip to content

Instantly share code, notes, and snippets.

@joostbroekhuizen
Last active January 17, 2020 15:56
Show Gist options
  • Save joostbroekhuizen/8ee387678ef7ac2e702f461d4af664ba to your computer and use it in GitHub Desktop.
Save joostbroekhuizen/8ee387678ef7ac2e702f461d4af664ba to your computer and use it in GitHub Desktop.
Custom Sitecore JSS Layout Processor
/// <summary>
/// Extends the LayoutService context with localized errors.
/// </summary>
public class ErrorsLayoutServiceContextProcessor : JssGetLayoutServiceContextProcessor
{
public ErrorsLayoutServiceContextProcessor(IConfigurationResolver configurationResolver) : base(configurationResolver)
{
}
protected override void DoProcess(GetLayoutServiceContextArgs args, AppConfiguration application)
{
if(args.RenderedItem == null || application == null)
{
return;
}
var errorItems = Sitecore.Context.Database?.GetItem(application.Attributes["generalErrorsRoot"])?.Children;
if (errorItems == null)
{
return;
}
var errors = new Dictionary<string, ErrorDTO>();
foreach (Item item in errorItems)
{
var code = item.Fields[Templates.Error.Fields.Code]?.Value;
if (string.IsNullOrEmpty(code))
{
continue;
}
errors.Add(code, new ErrorDTO()
{
Description = item.Fields[Templates.Error.Fields.Description]?.Value,
Message = item.Fields[Templates.Error.Fields.Message]?.Value,
Type = item.Fields[Templates.Error.Fields.Type]?.Value
});
}
args.ContextData.Add("errors", errors);
}
private class ErrorDTO
{
public string Description { get; set; }
public string Message { get; set; }
public string Type { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment