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
@@ -1,13 +1,18 @@
|
||||
using Elternbeirat.Contracts;
|
||||
using Elternbeirat.PocketBase;
|
||||
using Elternbeirat.Web.Shared;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace Elternbeirat.Web.Features.Faqs;
|
||||
|
||||
public partial class FaqList
|
||||
{
|
||||
[LoggerMessage(Level = LogLevel.Warning, Message = "Could not load FAQs from PocketBase.")]
|
||||
private static partial void LogLoadFailed(ILogger logger, Exception exception);
|
||||
[LoggerMessage(
|
||||
Level = LogLevel.Error,
|
||||
Message = "Could not load FAQs from PocketBase; content source is unavailable.")]
|
||||
private static partial void LogSourceUnavailable(
|
||||
ILogger logger,
|
||||
Exception exception);
|
||||
|
||||
// German heading per topic. Unknown topics fall back to the raw key so a new
|
||||
// topic in PocketBase still shows up rather than being silently dropped.
|
||||
@@ -21,28 +26,38 @@ public partial class FaqList
|
||||
};
|
||||
|
||||
[Inject]
|
||||
private PocketBaseClient PocketBase { get; set; } = default!;
|
||||
private PocketBaseClient PocketBase { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private ILogger<FaqList> Logger { get; set; } = default!;
|
||||
private ILogger<FaqList> Logger { get; set; } = null!;
|
||||
|
||||
[CascadingParameter]
|
||||
private HttpContext? HttpContext { get; set; }
|
||||
|
||||
private IReadOnlyList<IGrouping<string, Faq>> _groups = [];
|
||||
|
||||
// Set when the content source could not be reached or read. The markup then
|
||||
// shows a "temporarily unavailable" note instead of "no questions", matching
|
||||
// the 503 this sets on the response.
|
||||
private bool _unavailable;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var faqs = await PocketBase.GetFaqsAsync();
|
||||
var faqs = await PocketBase.GetFaqsAsync(HttpContext?.RequestAborted ?? CancellationToken.None);
|
||||
_groups = [.. faqs.GroupBy(faq => faq.Topic)];
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
catch (PocketBaseUnavailableException exception)
|
||||
{
|
||||
// Source unreachable -> show an empty page instead of a 500. The page
|
||||
// stays up; the "keine Fragen" note is rendered.
|
||||
LogLoadFailed(Logger, ex);
|
||||
// An empty page would claim there are no questions. The site works, its
|
||||
// data store is down: 503 and an honest note.
|
||||
LogSourceUnavailable(Logger, exception);
|
||||
_unavailable = true;
|
||||
SourceUnavailable.SetStatus(HttpContext);
|
||||
}
|
||||
}
|
||||
|
||||
private static string TopicLabel(string topic) =>
|
||||
TopicLabels.TryGetValue(topic, out var label) ? label : topic;
|
||||
private static string TopicLabel(string topic)
|
||||
=> TopicLabels.GetValueOrDefault(topic, topic);
|
||||
}
|
||||
Reference in new issue
Block a user