This commit is contained in:
biss committed 2026-08-29 11:24:05 +08:00
1 parent a1f48945e8
commit ab935480b0
26 files changed
+829 -5

No files matched your search

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
namespace ElectionSystem.Api.Common.Errors;
public sealed class GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger) : IExceptionHandler
{
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
{
var traceId = httpContext.TraceIdentifier;
logger.LogError(exception, "Unhandled request failure. TraceId: {TraceId}", traceId);
var problem = new ProblemDetails
{
Status = StatusCodes.Status500InternalServerError,
Title = "An unexpected error occurred.",
Type = "https://tools.ietf.org/html/rfc9110#section-15.6.1",
Instance = httpContext.Request.Path
};
problem.Extensions["traceId"] = traceId;
httpContext.Response.StatusCode = problem.Status.Value;
await httpContext.Response.WriteAsJsonAsync(problem, cancellationToken);
return true;
}
}
@@ -0,0 +1,14 @@
using System.Text.Json;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace ElectionSystem.Api.Common.Errors;
public static class HealthCheckResponseWriter
{
public static Task WriteAsync(HttpContext context, HealthReport report)
{
context.Response.ContentType = "application/json";
var payload = new { status = report.Status.ToString(), checks = report.Entries.ToDictionary(entry => entry.Key, entry => entry.Value.Status.ToString()) };
return context.Response.WriteAsync(JsonSerializer.Serialize(payload));
}
}
@@ -0,0 +1,6 @@
namespace ElectionSystem.Api.Common.Pagination;
public sealed record PagedResult<T>(IReadOnlyCollection<T> Items, int Page, int PageSize, long TotalCount)
{
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
}