From 3b7ab9a47ef7ffc81b5f5a3c69468bc2c992d116 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 9 Aug 2024 16:39:26 +0800 Subject: [PATCH 1/3] 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); From 6ae066d1e99fe3bfd0f82cb72fed24f35cede756 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 14 Aug 2024 14:44:41 +0800 Subject: [PATCH 2/3] Add OnSectionRenderingErrorAsync and slow down with crawling --- .../Pages/Documents/Project/Index.cshtml.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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 7789582f81..895e1c3085 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 @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -549,12 +550,7 @@ namespace Volo.Docs.Pages.Documents.Project } 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 - }); + await OnSectionRenderingErrorAsync(e); } } @@ -578,6 +574,19 @@ namespace Volo.Docs.Pages.Documents.Project Document.Content = content; } + + protected async Task OnSectionRenderingErrorAsync(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 + }); + + // Slow down with crawling + HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; + } private async Task> GetDocumentPartialTemplatesAsync() { From c72c30ba71f39e6ccb978ec371d54e0f1918fe61 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 14 Aug 2024 14:55:23 +0800 Subject: [PATCH 3/3] Update Index.cshtml.cs --- .../src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 895e1c3085..109aed3ed4 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 @@ -575,7 +575,7 @@ namespace Volo.Docs.Pages.Documents.Project Document.Content = content; } - protected async Task OnSectionRenderingErrorAsync(Exception e) + protected virtual async Task OnSectionRenderingErrorAsync(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}````";