Serve 503 when PocketBase is unavailable across all content pages
This commit is contained in:
1 parent
0f7e968c16
commit
e242eaf79a
25 files changed
+492
-253
No files matched your search
@@ -8,9 +8,9 @@ namespace Elternbeirat.Web.Features.Pages;
|
||||
public partial class ContentPage
|
||||
{
|
||||
[LoggerMessage(
|
||||
Level = LogLevel.Warning,
|
||||
Message = "Could not load page '{slug}' from PocketBase.")]
|
||||
private static partial void LogLoadFailed(
|
||||
Level = LogLevel.Error,
|
||||
Message = "Could not load page '{Slug}' from PocketBase; content source is unavailable.")]
|
||||
private static partial void LogSourceUnavailable(
|
||||
ILogger logger,
|
||||
string slug,
|
||||
Exception exception);
|
||||
@@ -24,11 +24,19 @@ public partial class ContentPage
|
||||
[Inject]
|
||||
private ILogger<ContentPage> Logger { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private NavigationManager Navigation { get; set; } = null!;
|
||||
|
||||
[CascadingParameter]
|
||||
private HttpContext? HttpContext { get; set; }
|
||||
|
||||
private Page? _page;
|
||||
|
||||
// Set when the content source could not be reached or read. The markup then
|
||||
// shows a "temporarily unavailable" note instead of a not-found page, matching
|
||||
// the 503 this sets on the response.
|
||||
private bool _unavailable;
|
||||
|
||||
// The page body rendered from Markdown to HTML, ready to emit as a
|
||||
// MarkupString in the markup.
|
||||
private MarkupString Body => (MarkupString)Markdown.ToHtml(_page?.Body);
|
||||
@@ -37,24 +45,23 @@ public partial class ContentPage
|
||||
{
|
||||
try
|
||||
{
|
||||
var pages = await PocketBase.GetPagesAsync();
|
||||
var pages = await PocketBase.GetPagesAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
|
||||
_page = pages.FirstOrDefault(
|
||||
p => string.Equals(p.Slug, Slug, StringComparison.OrdinalIgnoreCase));
|
||||
page => string.Equals(page.Slug, Slug, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
catch (PocketBaseUnavailableException exception)
|
||||
{
|
||||
// Source unreachable -> treat as "not found" rather than a 500. The
|
||||
// 404 branch below serves the friendly /not-found page.
|
||||
LogLoadFailed(Logger, Slug, ex);
|
||||
_page = null;
|
||||
// The site works, its data store is down: 503, not 404.
|
||||
LogSourceUnavailable(Logger, Slug, exception);
|
||||
_unavailable = true;
|
||||
SourceUnavailable.SetStatus(HttpContext);
|
||||
return;
|
||||
}
|
||||
|
||||
// Unknown slug (or unreachable source) -> 404, so
|
||||
// UseStatusCodePagesWithReExecute serves the /not-found page instead of an
|
||||
// empty 200 response.
|
||||
if (_page is null && HttpContext is not null)
|
||||
if (_page is null)
|
||||
{
|
||||
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
// The source answered, but there is no page with this slug: a genuine 404.
|
||||
Navigation.NotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user