Serve 503 when PocketBase is unavailable across all content pages

This commit is contained in:
tleininger committed 2026-09-24 13:00:14 +02:00
1 parent 0f7e968c16
commit e242eaf79a
25 files changed
+492 -253

No files matched your search

@@ -1,19 +1,25 @@
@page "/posts/{Slug}"
@if (_post is null)
@if (_unavailable)
{
<PageTitle>Nicht gefunden</PageTitle>
<PageTitle>Vorübergehend nicht erreichbar</PageTitle>
<p>Der Beitrag ist gerade nicht erreichbar. Bitte später erneut versuchen.</p>
}
else
else if (_post is not null)
{
<PageTitle>@_post.Title</PageTitle>
<nav>
<a href="/posts">&larr; Alle Beiträge</a>
</nav>
<article>
<p class="post-meta">
<a href="/posts">&larr; Alle Beiträge</a>
<time datetime="@_post.Date.ToString("yyyy-MM-dd")">
@_post.Date.ToString("d. MMMM yyyy", German)
<header>
<h1>@_post.Title</h1>
<time class="post-meta" datetime="@_post.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)">
@_post.Date.ToString("d. MMMM yyyy", Cultures.German)
</time>
</p>
</header>
@Body
</article>
}
@* No post and not unavailable: the slug was unknown; NavigationManager.NotFound()
already ran in OnParametersSetAsync and renders the not-found page. *@
@@ -1,33 +1,42 @@
using System.Globalization;
using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Elternbeirat.Web.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
namespace Elternbeirat.Web.Features.Posts;
public partial class PostDetail
{
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load post '{slug}' from PocketBase.")]
private static partial void LogLoadFailed(ILogger logger, string slug, Exception exception);
[LoggerMessage(
Level = LogLevel.Error,
Message = "Could not load post '{Slug}' from PocketBase; content source is unavailable.")]
private static partial void LogSourceUnavailable(
ILogger logger,
string slug,
Exception exception);
[Parameter]
public string Slug { get; set; } = string.Empty;
[Inject]
private PocketBaseClient PocketBase { get; set; } = default!;
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<PostDetail> Logger { get; set; } = default!;
private ILogger<PostDetail> Logger { get; set; } = null!;
[Inject]
private NavigationManager Navigation { get; set; } = null!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private Post? _post;
// 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 post body rendered from Markdown to HTML, ready to emit as a
// MarkupString in the markup.
private MarkupString Body => (MarkupString)Markdown.ToHtml(_post?.Body);
@@ -36,24 +45,23 @@ public partial class PostDetail
{
try
{
var posts = await PocketBase.GetPostsAsync();
var posts = await PocketBase.GetPostsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
_post = posts.FirstOrDefault(
p => string.Equals(p.Slug, Slug, StringComparison.OrdinalIgnoreCase));
post => string.Equals(post.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);
_post = 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 (_post is null && HttpContext is not null)
if (_post is null)
{
HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
// The source answered, but there is no post with this slug: a genuine 404.
Navigation.NotFound();
}
}
}
@@ -4,7 +4,11 @@
<h1>Beiträge</h1>
@if (_posts.Count == 0)
@if (_unavailable)
{
<p>Die Beiträge sind gerade nicht erreichbar. Bitte später erneut versuchen.</p>
}
else if (_posts.Count == 0)
{
<p>Zurzeit gibt es keine Beiträge.</p>
}
@@ -15,8 +19,8 @@ else
{
<li>
<a href="@($"/posts/{post.Slug}")">@post.Title</a>
<time datetime="@post.Date.ToString("yyyy-MM-dd")">
@post.Date.ToString("d. MMMM yyyy", German)
<time datetime="@post.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)">
@post.Date.ToString("d. MMMM yyyy", Cultures.German)
</time>
</li>
}
@@ -1,36 +1,48 @@
using System.Globalization;
using Elternbeirat.Contracts;
using Elternbeirat.PocketBase;
using Elternbeirat.Web.Shared;
using Microsoft.AspNetCore.Components;
namespace Elternbeirat.Web.Features.Posts;
public partial class PostList
{
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load posts from PocketBase.")]
private static partial void LogLoadFailed(ILogger logger, Exception exception);
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
[LoggerMessage(
Level = LogLevel.Error,
Message = "Could not load posts from PocketBase; content source is unavailable.")]
private static partial void LogSourceUnavailable(
ILogger logger,
Exception exception);
[Inject]
private PocketBaseClient PocketBase { get; set; } = default!;
private PocketBaseClient PocketBase { get; set; } = null!;
[Inject]
private ILogger<PostList> Logger { get; set; } = default!;
private ILogger<PostList> Logger { get; set; } = null!;
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private IReadOnlyList<Post> _posts = [];
// Set when the content source could not be reached or read. The markup then
// shows a "temporarily unavailable" note instead of "no posts", matching the
// 503 this sets on the response.
private bool _unavailable;
protected override async Task OnInitializedAsync()
{
try
{
_posts = await PocketBase.GetPostsAsync();
_posts = await PocketBase.GetPostsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
}
catch (HttpRequestException ex)
catch (PocketBaseUnavailableException exception)
{
// Source unreachable -> show an empty list instead of a 500. The page
// stays up; the "keine Beiträge" note is rendered.
LogLoadFailed(Logger, ex);
// An empty list would claim there are no posts. The site works, its
// data store is down: 503 and an honest note.
LogSourceUnavailable(Logger, exception);
_unavailable = true;
SourceUnavailable.SetStatus(HttpContext);
}
}
}