From 3b7ab9a47ef7ffc81b5f5a3c69468bc2c992d116 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 9 Aug 2024 16:39:26 +0800 Subject: [PATCH] Add DocumentRenderErrorEvent --- .../Docs/Documents/DocumentRenderErrorEvent.cs | 8 ++++++++ .../Pages/Documents/Project/Index.cshtml.cs | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentRenderErrorEvent.cs diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentRenderErrorEvent.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentRenderErrorEvent.cs new file mode 100644 index 0000000000..2f30099576 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentRenderErrorEvent.cs @@ -0,0 +1,8 @@ +namespace Volo.Docs.Documents; + +public class DocumentRenderErrorEvent +{ + public string Name { get; set; } + + public string ErrorMessage { get; set; } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index c3c1ae9643..7789582f81 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -16,6 +16,7 @@ using Volo.Abp.Application.Dtos; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; using Volo.Abp.Data; using Volo.Abp.Domain.Entities; +using Volo.Abp.EventBus.Local; using Volo.Docs.Documents; using Volo.Docs.HtmlConverting; using Volo.Docs.Models; @@ -96,6 +97,8 @@ namespace Volo.Docs.Pages.Documents.Project private readonly DocsUiOptions _uiOptions; protected IDocsLinkGenerator DocsLinkGenerator => LazyServiceProvider.LazyGetRequiredService(); + + protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService(); public IndexModel( IDocumentAppService documentAppService, @@ -540,7 +543,19 @@ namespace Volo.Docs.Pages.Documents.Project DocumentNavigationsDto = await _documentSectionRenderer.GetDocumentNavigationsAsync(Document.Content); - Document.Content = await _documentSectionRenderer.RenderAsync(Document.Content, UserPreferences, partialTemplates); + try + { + Document.Content = await _documentSectionRenderer.RenderAsync(Document.Content, UserPreferences, partialTemplates); + } + catch (Exception e) + { + var message = $"Error occurred during the rendering of this document. The document is not valid: {e.Message}"; + Document.Content = $"````txt{Environment.NewLine}{message}{Environment.NewLine}````"; + await LocalEventBus.PublishAsync(new DocumentRenderErrorEvent + { + ErrorMessage = message, Name = Document.Name + }); + } } var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format);