Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DocumentRenderErrorEvent #20463

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Volo.Docs.Documents;

public class DocumentRenderErrorEvent
{
public string Name { get; set; }

public string ErrorMessage { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@
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;
Expand Down Expand Up @@ -96,6 +98,8 @@ public class IndexModel : AbpPageModel
private readonly DocsUiOptions _uiOptions;

protected IDocsLinkGenerator DocsLinkGenerator => LazyServiceProvider.LazyGetRequiredService<IDocsLinkGenerator>();

protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();

public IndexModel(
IDocumentAppService documentAppService,
Expand Down Expand Up @@ -540,7 +544,14 @@ private async Task ConvertDocumentContentToHtmlAsync()

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)
{
await OnSectionRenderingErrorAsync(e);
}
}

var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format);
Expand All @@ -563,6 +574,19 @@ private async Task ConvertDocumentContentToHtmlAsync()

Document.Content = content;
}

protected async Task OnSectionRenderingErrorAsync(Exception e)
realLiangshiwei marked this conversation as resolved.
Show resolved Hide resolved
{
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<List<DocumentPartialTemplateContent>> GetDocumentPartialTemplatesAsync()
{
Expand Down
Loading